HEAD: remove ridiculous dependency of wvmapi (actually wvtnef) on evolution
[wvapps.git] / wvprint / wvprintconfwatcher.cc
blob928c5490efcc2a79df3225c71ba7dfdf69d6bd61
4 #include "wvlog.h"
5 #include "uniconf.h"
6 #include "wvprint.h"
7 #include "wvcupsmanager.h"
9 #include "wvprintconfwatcher.h"
11 #include "wvprintuniconfkeys.h"
15 WvPrintConfWatcher::WvPrintConfWatcher(WvLog &_log, const UniConf &_cfg)
16 : log(_log), cfg(_cfg), queues(16), cups(NULL)
18 /* needed for editing of the 'WvPrint/aliases' on the commandline */
19 watches.add(cfg[WVPRINT_ALIASES_KEYS],
20 UniConfCallback(this, &WvPrintConfWatcher::addalias_callback), true);
22 /* handle all the queues thru UniCallbacks */
23 watches.add(cfg[WVPRINT_QUEUES_KEYS],
24 UniConfCallback(this, &WvPrintConfWatcher::addqueue_callback), true);
28 void WvPrintConfWatcher::addalias_callback(const UniConf &_cfg, const UniConfKey &_key)
30 /* if the user has invoked this callback .. not when we're doing a config
31 * aliases. Also, don't let them delete the lp alias */
32 // if (!need_alias_config)
33 // return;
35 WvString queuename = _cfg[_key].getme("");
36 if (!!queuename && queues[queuename])
38 //configure_aliases();
40 else
42 _cfg[_key].remove();
47 void WvPrintConfWatcher::addqueue_callback(const UniConf &_cfg, const UniConfKey &_key)
49 log(WvLog::Debug1, "addqueue_callback. Key = %s\n", _key);
51 if (!!_key.printable())
53 WvStringList subkeys;
54 subkeys.split(_key, "/");
55 if (subkeys.count() == 1)
57 _add_queue(*subkeys.first());
59 else
61 _update_queue(*subkeys.first());
67 void WvPrintConfWatcher::init_config()
69 log(WvLog::Debug1, "init_config()\n");
70 if(!queues.isempty())
72 log(WvLog::Warning, "queues is not empty. Emptying\n");
73 queues.zap();
76 UniConf::Iter cfgit(cfg[WVPRINT_QUEUES_KEYS]);
78 /* Iterate over all queues .. starts only the real ones */
79 for (cfgit.rewind(); cfgit.next(); )
81 WvString queuename(cfgit().key().printable());
82 log(WvLog::Debug1, "adding a queue\n");
83 _add_queue(queuename);
87 WvString WvPrintConfWatcher::get_printer_state(WvStringParm queuename)
89 WvString state = "";
90 PrintQueueName *qn = queues[queuename];
91 if (qn)
93 state = qn->queue->get_state();
95 return state;
99 int WvPrintConfWatcher::count_jobs(WvStringParm queuename)
101 int count = 0;
102 PrintQueueName *qn = queues[queuename];
103 if (qn)
105 count = qn->queue->job_count();
107 return count;
111 bool WvPrintConfWatcher::job_exists(WvStringParm queuename, int job_num)
113 bool found = false;
114 PrintQueueName *qn = queues[queuename];
115 if (qn)
117 found = qn->queue->job_exists(queuename, job_num);
119 return found;
123 bool WvPrintConfWatcher::cancel_job(WvStringParm queuename, int job_num)
125 PrintQueueName *qn = queues[queuename];
126 if (qn)
128 return qn->queue->cancel(job_num);
130 return false;
135 void WvPrintConfWatcher::_add_queue(WvStringParm name)
137 PrintQueue *queue = PrintQueue::new_queue(name, cfg, log);
139 if (queue)
141 queue->set_cups_manager(cups);
142 queues.add(new PrintQueueName(queue->name, queue), true);
143 log(WvLog::Debug, "Added new queue %s.\n", name);
144 queue->enable();
146 else
148 log(WvLog::Error, "Could not add new queue %s; disabling.\n", name);
153 void WvPrintConfWatcher::_update_queue(WvStringParm queuename)
155 PrintQueue *queue = queues[queuename]->queue;
157 // FIXME: looks like this can happen to often. Perhaps should we add the queue
158 // to a list and reconfig every once and while
159 if (cups)
161 queue->reconfig();
166 void WvPrintConfWatcher::_remove_queue(WvStringParm queuename)
168 /* Clean up both queue and uniconf */
169 PrintQueueName *q = queues[queuename];
171 if (q)
173 log(WvLog::Debug, "Removing queue %s\n", queuename);
174 queues.remove(q);
176 else
178 log(WvLog::Debug, "Could not find queue %s\n", queuename);