List all printers, not the best way
This commit is contained in:
Moritz Lipp 2010-01-02 00:25:18 +01:00
parent c55ca998d3
commit 6d1d3901c6
2 changed files with 72 additions and 1 deletions

View file

@ -32,6 +32,9 @@ static const char notification_w_fgcolor[] = "#000000";
/* statusbar */
static const char DEFAULT_TEXT[] = "[No Name]";
/* printing */
#define LIST_PRINTER_COMMAND "lpstat -v | sed -n '/^.*device for \\(.*\\): .*$/s//\\1/p'"
/* additional settings */
#define SHOW_SCROLLBARS 0
@ -82,7 +85,7 @@ Command commands[] = {
/* command, abbreviation, function, completion, description */
{"close", "c", cmd_close, 0, "Close current file" },
{"open", "o", cmd_open, cc_open, "Open a file" },
{"print", "p", cmd_print, 0, "Print the document" },
{"print", "p", cmd_print, cc_print, "Print the document" },
{"rotate", "r", cmd_rotate, 0, "Rotate the page" },
{"set", "s", cmd_set, cc_set, "Set an option" },
{"quit", "q", cmd_quit, 0, "Quit zjui" },

View file

@ -250,6 +250,7 @@ gboolean cmd_zoom(int, char**);
/* completion commands */
Completion* cc_open(char*);
Completion* cc_print(char*);
Completion* cc_set(char*);
/* buffer command declarations */
@ -1528,6 +1529,73 @@ cc_open(char* input)
return completion;
}
Completion*
cc_print(char* input)
{
/* init completion group */
Completion *completion = malloc(sizeof(Completion));
CompletionGroup* group = malloc(sizeof(CompletionGroup));
group->value = NULL;
group->next = NULL;
group->elements = NULL;
completion->groups = group;
CompletionElement *last_element = NULL;
int element_counter = 0;
int input_length = input ? strlen(input) : 0;
/* read printers */
char *current_line = NULL, current_char;
int count = 0;
FILE *fp;
fp = popen(LIST_PRINTER_COMMAND, "r");
if(!fp)
return NULL;
while((current_char = fgetc(fp)) != EOF)
{
if(!current_line)
current_line = malloc(sizeof(char) * 512);
current_line = realloc(current_line, (count + 1) * sizeof(char));
if(current_char != '\n')
current_line[count++] = current_char;
else
{
current_line[count] = '\0';
int line_length = strlen(current_line);
if( (input_length <= line_length) ||
(!strncmp(input, current_line, input_length)) )
{
CompletionElement* el = malloc(sizeof(CompletionElement));
el->value = g_strdup(current_line);
el->description = NULL;
el->next = NULL;
if(element_counter++ != 0)
last_element->next = el;
else
group->elements = el;
last_element = el;
}
free(current_line);
current_line = NULL;
count = 0;
}
}
pclose(fp);
return completion;
}
Completion*
cc_set(char* input)
{