Skip to content

Commit 17a1d82

Browse files
API functions to refresh printer list and to get D-Bus connection (#35)
- Added API function cpdbRefreshPrinterList() to refresh the printer list - Turned static function get_dbus_connection() into API function cpdbGetDbusConnection()
1 parent eb759dc commit 17a1d82

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

cpdb/cpdb-frontend.c

+66-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static void on_printer_state_changed(GDBusConnection *connection,
160160
f->printer_cb(f, p, CPDB_CHANGE_PRINTER_STATE_CHANGED);
161161
}
162162

163-
static GDBusConnection *get_dbus_connection()
163+
GDBusConnection *cpdbGetDbusConnection()
164164
{
165165
gchar *bus_addr;
166166
GError *error = NULL;
@@ -190,7 +190,7 @@ void cpdbConnectToDBus(cpdb_frontend_obj_t *f)
190190
GMainContext *context;
191191
GError *error = NULL;
192192

193-
if ((f->connection = get_dbus_connection()) == NULL)
193+
if ((f->connection = cpdbGetDbusConnection()) == NULL)
194194
{
195195
loginfo("Couldn't connect to DBus\n");
196196
return;
@@ -291,6 +291,69 @@ static void fetchPrinterListFromBackend(cpdb_frontend_obj_t *f, const char *back
291291
}
292292
}
293293

294+
bool cpdbRefreshPrinterList(cpdb_frontend_obj_t *f, char *backend)
295+
{
296+
int num_printers;
297+
GVariantIter iter;
298+
GVariant *printers, *printer;
299+
PrintBackend *proxy;
300+
GError *error = NULL;
301+
cpdb_printer_obj_t *p;
302+
303+
if ((proxy = g_hash_table_lookup(f->backend, backend)) == NULL)
304+
{
305+
logerror("Couldn't get %s proxy object\n", backend);
306+
return false;
307+
}
308+
print_backend_call_get_all_printers_sync (proxy, &num_printers,
309+
&printers, NULL, &error);
310+
if (error)
311+
{
312+
logerror("Error getting %s printer list : %s\n", backend, error->message);
313+
return false;
314+
}
315+
logdebug("Fetched %d printers from backend %s\n", num_printers, backend);
316+
g_variant_iter_init(&iter, printers);
317+
while (g_variant_iter_loop(&iter, "(v)", &printer))
318+
{
319+
p = cpdbGetNewPrinterObj();
320+
cpdbFillBasicOptions(p, printer);
321+
if (f->last_saved_settings != NULL)
322+
cpdbCopySettings(f->last_saved_settings, p->settings);
323+
cpdbAddPrinter(f, p);
324+
}
325+
326+
GHashTableIter iterator;
327+
gpointer key, value;
328+
329+
g_hash_table_iter_init(&iterator, f->printer);
330+
331+
while (g_hash_table_iter_next(&iterator, &key, &value)) {
332+
cpdb_printer_obj_t* printer_obj = (cpdb_printer_obj_t*)value;
333+
char* backend_name = printer_obj->backend_name;
334+
335+
// Compare the backend_name with the provided one
336+
if (strcmp(backend_name, backend) == 0) {
337+
// Check if backend_name is not in the printers hashtable
338+
char *printer_name = cpdbConcatSep(printer_obj->id, backend_name);
339+
g_variant_iter_init(&iter, printers);
340+
int printer_exists = 0;
341+
while (g_variant_iter_loop(&iter, "(v)", &printer))
342+
{
343+
cpdb_printer_obj_t *temp = cpdbGetNewPrinterObj();
344+
cpdbFillBasicOptions(temp, printer);
345+
if (strcmp(temp->name, printer_obj->name) == 0){
346+
printer_exists = 1;
347+
break;
348+
}
349+
cpdbDeletePrinterObj(temp);
350+
}
351+
if(printer_exists == 0) cpdbRemovePrinter(f, printer_obj->id, backend_name);
352+
}
353+
}
354+
return true;
355+
}
356+
294357
// Helper function to add existing backends to a hash table
295358
static void add_to_hash_table(gpointer key, gpointer value, gpointer user_data) {
296359
GHashTable *hash_table = (GHashTable *)user_data;
@@ -1307,7 +1370,7 @@ cpdb_printer_obj_t *cpdbResurrectPrinterFromFile(const char *filename)
13071370
p->backend_name = cpdbGetStringCopy(strtok(buf, "#"));
13081371

13091372
service_name = cpdbConcat(CPDB_BACKEND_PREFIX, p->backend_name);
1310-
if ((connection = get_dbus_connection()) == NULL)
1373+
if ((connection = cpdbGetDbusConnection()) == NULL)
13111374
{
13121375
logerror("Error resurrecting printer : Couldn't get dbus connection\n");
13131376
goto failed;

cpdb/cpdb-frontend.h

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _CPDB_CPDB_FRONTEND_H_
33

44
#include <glib.h>
5+
#include <stdbool.h>
56

67
#ifdef __cplusplus
78
extern "C"
@@ -116,6 +117,8 @@ void cpdbDeleteFrontendObj(cpdb_frontend_obj_t *frontend_obj);
116117
*/
117118
void cpdbConnectToDBus(cpdb_frontend_obj_t *frontend_obj);
118119

120+
GDBusConnection *cpdbGetDbusConnection();
121+
119122
/**
120123
* Disconnect from the DBus.
121124
*
@@ -400,6 +403,7 @@ int cpdbPrintFD(cpdb_printer_obj_t *p, char **jobid, const char *title, char **s
400403

401404
char *cpdbPrintSocket(cpdb_printer_obj_t *p, char **jobid, const char *title);
402405

406+
bool cpdbRefreshPrinterList(cpdb_frontend_obj_t *f, char *backend);
403407

404408
/**
405409
* Set an option value for a printer.

0 commit comments

Comments
 (0)