minor fix
[opensuse:kupdateapplet.git] / updater / packagekit / QPackageKitConnector.cpp
1 #include "QPackageKitConnector.h"
2 #include "kdebug.h"
3
4 QPackageKitConnector::QPackageKitConnector( QObject *parent ) 
5         : QObject( parent )
6         , _lastAction("none")
7         , _lastActionArg(false)
8 {
9 }
10
11
12 void QPackageKitConnector::disconnectFromInterface(QPackageKitInterface* transactioninterface)
13 {
14         disconnect(transactioninterface, 
15                 SIGNAL( Package(const QString, const QString, const QString) ), 
16                 this,
17                 SLOT( proxy_Package(const QString, const QString, const QString) )
18         );
19
20         disconnect(transactioninterface,
21                 SIGNAL( TransactionFinished(const QString, uint, QPackageKitInterface*) ),
22                 this,
23                 SLOT( proxy_Finished(const QString, uint, QPackageKitInterface*) )
24         );
25
26         disconnect(transactioninterface,
27                 SIGNAL( ProgressChanged(uint, uint, uint, uint) ),
28                 this,
29                 SLOT( proxy_ProgressChanged(uint,uint, uint, uint) )
30         );
31
32         disconnect(transactioninterface, 
33                 SIGNAL( Description(const QString&, const QString&, const QString&,
34                                  const QString&, const QString&, qulonglong)),
35                 this,
36                 SLOT( proxy_Description(const QString&, const QString&, const QString&,
37                                 const QString&, const QString&, qulonglong))
38         );
39
40         disconnect(transactioninterface, 
41                 SIGNAL( ErrorCode(const QString&, const QString& )),
42                 this,
43                 SLOT( Error( const QString&, const QString&))
44         );
45
46         
47         disconnect(transactioninterface,
48                 SIGNAL ( RepoSignatureRequired(const QString&,  const QString&,
49                         const QString&, const QString&, const QString&,
50                         const QString&, const QString&, const QString&)),
51                 this,
52                 SLOT (proxy_RepoSignatureRequired(const QString&, const QString&,
53                         const QString&, const QString&, const QString &,
54                         const QString&, const QString&, const QString &))
55         );
56
57
58         disconnect(transactioninterface,
59                 SIGNAL( EulaRequired(const QString&, const QString&, const QString&,
60                                 const QString&)),
61                 this,
62                 SLOT( proxy_EulaRequired(const QString&, const QString&, const QString&,
63                                 const QString&))
64         );
65
66         disconnect(transactioninterface,
67                 SIGNAL (RepoDetail(const QString&, const QString&, bool)),
68                 this,
69                 SLOT(proxy_RepoDetail(const QString&, const QString &, bool))
70         );
71
72         transactioninterface->deleteLater();
73
74 }
75
76 void QPackageKitConnector::Error(const QString& code, const QString& message)
77 {
78         emit ErrorMessage("PackageKit Error " + code + ": " + message);  
79 }
80
81 QPackageKitInterface* QPackageKitConnector::connectToInterface()
82 {
83
84         QString tid = getTid();
85         if (tid == QString())   // tid is not valid
86         {
87                 emit ErrorMessage( 
88                         "DBus error : Unable to get transaction id  from packagekit. DBUS/PackageKit not running? "
89                                  );
90                 return NULL;
91         }
92
93         QPackageKitInterface *transactioninterface = new QPackageKitInterface(PK_DBUS_SERVICE, 
94                                                         tid,
95                                                         PK_DBUS_TRANSACTION_INTERFACE,
96                                                         QDBusConnection::systemBus() );
97
98         connect(transactioninterface, 
99                 SIGNAL( Package(const QString, const QString, const QString) ), 
100                 this,
101                 SLOT( proxy_Package(const QString, const QString, const QString) )
102         );
103
104         connect(transactioninterface,
105                 SIGNAL( TransactionFinished(const QString, uint, QPackageKitInterface*) ),
106                 this,
107                 SLOT( proxy_Finished(const QString, uint, QPackageKitInterface*) )
108         );
109
110         connect(transactioninterface,
111                 SIGNAL( ProgressChanged(uint, uint, uint, uint) ),
112                 this,
113                 SLOT( proxy_ProgressChanged(uint,uint, uint, uint) )
114         );
115
116         connect(transactioninterface, 
117                 SIGNAL( Description(const QString&, const QString&, const QString&,
118                                  const QString&, const QString&, qulonglong)),
119                 this,
120                 SLOT( proxy_Description(const QString&, const QString&, const QString&,
121                                 const QString&, const QString&, qulonglong))
122         );
123
124         connect(transactioninterface, 
125                 SIGNAL( ErrorCode(const QString&, const QString& )),
126                 this,
127                 SLOT( Error( const QString&, const QString&))
128         );
129
130         connect(transactioninterface,
131                 SIGNAL ( RepoSignatureRequired(const QString&,  const QString&,
132                         const QString&, const QString&, const QString&,
133                         const QString&, const QString&, const QString&)),
134                 this,
135                 SLOT (proxy_RepoSignatureRequired(const QString&, const QString&,
136                         const QString&, const QString&, const QString &,
137                         const QString&, const QString&, const QString &))
138         );
139
140
141         connect(transactioninterface,
142                 SIGNAL( EulaRequired(const QString&, const QString&, const QString&,
143                                 const QString&)),
144                 this,
145                 SLOT( proxy_EulaRequired(const QString&, const QString&, const QString&,
146                                 const QString&))
147         );
148
149         connect(transactioninterface,
150                 SIGNAL (RepoDetail(const QString&, const QString&, bool)),
151                 this,
152                 SLOT(proxy_RepoDetail(const QString&, const QString &, bool))
153         );
154
155
156
157         return transactioninterface;
158
159 }
160
161
162
163 void QPackageKitConnector::sendErrorMessage(QPackageKitInterface* interface)
164 {
165         emit ErrorMessage( "DBus error : " 
166                 + interface->lastError().name() +  " : " 
167                 + interface->lastError().message() );
168
169
170 }
171
172
173 bool QPackageKitConnector::getDescription(QString packageId)
174 {
175         QPackageKitInterface* transactioninterface = connectToInterface();
176
177         if (!transactioninterface)
178                 return false;
179
180         QDBusReply<void> reply = transactioninterface->GetDescription(packageId);
181         if(reply.isValid())
182         {
183                 return true;
184         }
185         else
186         {
187                 sendErrorMessage(transactioninterface);
188                 return false;
189         }
190 }
191
192 bool QPackageKitConnector::acceptEula(QString eulaId)
193 {
194         QPackageKitInterface* transactioninterface = connectToInterface();
195
196         if (!transactioninterface)
197                 return false;
198
199         QDBusReply<void> reply = transactioninterface->AcceptEula(eulaId);
200         if(reply.isValid())
201         {
202                 return true;
203         }
204         else
205         {
206                 sendErrorMessage(transactioninterface);
207                 return false;
208         }
209 }
210
211 bool QPackageKitConnector::getUpdates()
212 {
213         _lastAction    = QString("getUpdates");
214         _lastActionArg = false;
215
216         QPackageKitInterface* transactioninterface = connectToInterface();
217
218         if (!transactioninterface)
219                 return false;
220         QDBusReply<void> reply = transactioninterface->GetUpdates();
221         if(reply.isValid())
222         {
223                 kDebug() << "updates replay valid";
224                 return true;
225         }
226         else
227         {
228                 kDebug() << "updates replay not valid";
229                 sendErrorMessage(transactioninterface);
230                 return false;
231         }
232 }
233 bool QPackageKitConnector::getRepoList()
234 {
235         QPackageKitInterface* transactioninterface = connectToInterface();
236
237         if (!transactioninterface)
238                 return false;
239         QDBusReply<void> reply = transactioninterface->GetRepoList();
240         if(reply.isValid())
241         {
242                 kDebug() << "get repolist valid";
243                 return true;
244         }
245         else
246         {
247                 kDebug() << "get repolist not valid";
248                 sendErrorMessage(transactioninterface);
249                 return false;
250         }
251 }
252
253
254 bool QPackageKitConnector::refreshCache(bool force)
255 {
256         _lastAction    = QString("refreshCache");
257         _lastActionArg = force;
258
259         QPackageKitInterface* transactioninterface = connectToInterface();
260
261         if (!transactioninterface)
262                 return false;
263         QDBusReply<void> reply = transactioninterface->RefreshCache(force);
264         if(reply.isValid())
265         {
266                 kDebug() << "refresh replay valid";
267                 return true;
268         }
269         else
270         {
271                 kDebug() << "refresh replay not valid";
272                 sendErrorMessage(transactioninterface);
273                 return false;
274         }
275 }
276
277 QString QPackageKitConnector::getTid()
278 {
279         QPackageKitInterface *interface = new QPackageKitInterface( PK_DBUS_SERVICE
280                 , PK_DBUS_PATH , PK_DBUS_INTERFACE,  QDBusConnection::systemBus() );
281
282         QDBusReply<QString> tid = interface->GetTid();
283         if(tid.isValid())
284         {
285                 kDebug() << "tid: " << tid;
286                 return tid;
287         }
288         else
289         {
290                 sendErrorMessage(interface);
291                 return QString();
292         }
293         
294         delete interface;
295
296 }
297
298 bool QPackageKitConnector::triggerLastActionAgain()
299 {
300         kDebug() << "Starting last action again..."; 
301
302         if (_lastAction == QString("getUpdates"))
303                 return getUpdates();
304         else if (_lastAction    == QString("refreshCache"))
305                 return refreshCache(_lastActionArg.toBool());
306         else if (_lastAction    == QString("installPackage"))
307                 return installPackage(_lastActionArg.toString());
308         else if (_lastAction    == QString("updatePackages"))
309                 return updatePackages(_lastActionArg.toStringList());
310         else 
311                 return false;
312 }
313
314 bool QPackageKitConnector::installSignature(QString type, QString key_id, QString package_id)
315 {
316         QPackageKitInterface* transactioninterface = connectToInterface();
317
318         if (!transactioninterface)
319                 return false;
320
321         QDBusReply<void> reply = transactioninterface->InstallSignature(type, key_id, package_id);
322         if(reply.isValid())
323         {
324
325                 kDebug() << "install sig valid";
326                 return true;
327         }
328         else
329         {
330                 kDebug() << "install sig not valid";
331                 sendErrorMessage(transactioninterface);
332                 return false;
333         }
334
335 }
336
337 bool QPackageKitConnector::installPackage(QString packageId)
338 {
339         _lastAction    = QString("installPackage");
340         _lastActionArg = packageId;
341
342         QPackageKitInterface* transactioninterface = connectToInterface();
343
344         if (!transactioninterface)
345                 return false;
346
347         QDBusReply<void> reply = transactioninterface->InstallPackage(packageId);
348         if(reply.isValid())
349         {
350
351                 kDebug() << "install valid";
352                 return true;
353         }
354         else
355         {
356                 kDebug() << "install not valid";
357                 sendErrorMessage(transactioninterface);
358                 return false;
359         }
360 }
361
362 bool QPackageKitConnector::updatePackages(QStringList packageIds)
363 {
364         _lastAction    = QString("updatePackages");
365         _lastActionArg = packageIds;
366
367
368         QPackageKitInterface* transactioninterface = connectToInterface();
369
370         if (!transactioninterface)
371                 return false;
372
373         QDBusReply<void> reply = transactioninterface->UpdatePackages(packageIds);
374         if(reply.isValid())
375         {
376
377                 kDebug() << "updatepackages valid";
378                 return true;
379         }
380         else
381         {
382                 kDebug() << "updatepackages not valid";
383                 sendErrorMessage(transactioninterface);
384                 return false;
385         }
386 }
387
388
389
390 void QPackageKitConnector::proxy_Package( const QString &info, const QString &package_id, const QString &summary)
391 {
392 //      kDebug() << "Package:" << package_id;
393
394         emit Package(info, package_id, summary);
395 }
396
397 void QPackageKitConnector::proxy_Finished(const QString &status, uint runtime, QPackageKitInterface* transactioninterface)
398 {
399         kDebug() << "Finished";
400
401         if (transactioninterface->transactionType() == "GetUpdates")
402                 emit UpdatesFinished();
403
404         disconnectFromInterface(transactioninterface);
405         emit Finished(Exit::fromString(status), runtime);
406 }
407
408
409 void QPackageKitConnector::proxy_ProgressChanged(uint percentage, uint subpercentage, uint elapsed, uint remaining)
410 {
411         if(percentage == 101)
412         {
413                 emit NoProgressUpdates();
414         }
415         else
416         {
417                 emit ProgressChanged(percentage, subpercentage, elapsed, remaining);
418         }
419 }
420
421 void QPackageKitConnector::proxy_RepoSignatureRequired(const QString &package_id, const QString &repository_name,
422          const QString  &key_url, const QString &key_userid, const QString &key_id,
423          const QString &key_fingerprint, const QString &key_timestamp, const QString &type)
424 {
425         kDebug() << "proxy_RepoSignatureRequired";
426
427          emit RepoSignatureRequired(package_id,  repository_name, key_url, key_userid, 
428                 key_id, key_fingerprint, key_timestamp, type); 
429 }
430
431
432 void QPackageKitConnector::proxy_Description(const QString &package_id, const QString &licence, const QString &group, const QString &detail, const QString &url, qulonglong size) 
433 {
434         emit Description(package_id, licence, group, detail, url, size); 
435 }
436
437 void QPackageKitConnector::proxy_EulaRequired(const QString &eula_id, const QString &package_id, const QString &vendor_name, const QString &license_agreement)
438 {
439         emit EulaRequired(eula_id, package_id, vendor_name, license_agreement);
440 }       
441
442
443 void QPackageKitConnector::proxy_RepoDetail(const QString &repo_id, const QString &desc, bool enabled)
444 {
445         emit RepoDetail(repo_id, desc, enabled);
446 }