made packagekit plugin listen to message signal
[opensuse:kupdateapplet.git] / src / packagekit / qpackagekitconnector.cpp
1 #include "kdebug.h"
2
3 #include "qpackagekitinterface.h"
4 #include "qpackagekittransactioninterface.h"
5 #include "qpackagekitconnector.h"
6
7 QPackageKitConnector::QPackageKitConnector( QObject *parent ) 
8         : QObject( parent )
9         , _lastAction("none")
10         , _lastActionArg(false)
11 {
12 }
13
14
15 void QPackageKitConnector::disconnectFromInterface(QPackageKitTransactionInterface* transactioninterface)
16 {
17         disconnect(transactioninterface, 
18                 SIGNAL( Package(const QString, const QString, const QString) ), 
19                 this,
20                 SLOT( proxy_Package(const QString, const QString, const QString) )
21         );
22
23         disconnect(transactioninterface,
24                 SIGNAL( Finished(const QString, uint) ),
25                 this,
26                 SLOT( proxy_Finished(const QString, uint) )
27         );
28
29         disconnect(transactioninterface,
30                 SIGNAL( ProgressChanged(uint, uint, uint, uint) ),
31                 this,
32                 SLOT( proxy_ProgressChanged(uint,uint, uint, uint) )
33         );
34
35         disconnect(transactioninterface,
36                 SIGNAL( StatusChanged(const QString &) ),
37                 this,
38                 SLOT( proxy_StatusChanged(const QString &) )
39         );
40
41         disconnect(transactioninterface,
42              SIGNAL( Transaction(const QString &, const QString &, bool, const QString &, uint, const QString &) ),
43              this,
44              SLOT(proxy_Transaction(const QString &, const QString &, bool, const QString &, uint, const QString &) )
45         );
46
47         disconnect(transactioninterface, 
48                 SIGNAL( Details(const QString&, const QString&, const QString&,
49                                  const QString&, const QString&, qulonglong)),
50                 this,
51                 SLOT( proxy_Details(const QString&, const QString&, const QString&,
52                                 const QString&, const QString&, qulonglong))
53         );
54
55         disconnect (transactioninterface,
56                 SIGNAL( UpdateDetail(const QString&, const QString&, const QString&, const QString&, 
57                         const QString&, const QString&, const QString&, const QString&,
58                         const QString&, const QString&, const QString&, const QString&)),
59
60                 this,
61                 SLOT( proxy_UpdateDetail(const QString&, const QString&, const QString&, const QString&, 
62                         const QString&, const QString&, const QString&, const QString&,
63                         const QString&, const QString&, const QString&, const QString&))
64         );
65
66
67         disconnect(transactioninterface, 
68                 SIGNAL( ErrorCode(const QString&, const QString& )),
69                 this,
70                 SLOT( slotError( const QString&, const QString&))
71         );
72
73         
74         disconnect(transactioninterface,
75                 SIGNAL ( RepoSignatureRequired(const QString&,  const QString&,
76                         const QString&, const QString&, const QString&,
77                         const QString&, const QString&, const QString&)),
78                 this,
79                 SLOT (proxy_RepoSignatureRequired(const QString&, const QString&,
80                         const QString&, const QString&, const QString &,
81                         const QString&, const QString&, const QString &))
82         );
83
84
85         disconnect(transactioninterface,
86                 SIGNAL( EulaRequired(const QString&, const QString&, const QString&,
87                                 const QString&)),
88                 this,
89                 SLOT( proxy_EulaRequired(const QString&, const QString&, const QString&,
90                                 const QString&))
91         );
92
93         disconnect(transactioninterface,
94                 SIGNAL (RepoDetail(const QString&, const QString&, bool)),
95                 this,
96                 SLOT(proxy_RepoDetail(const QString&, const QString &, bool))
97         );
98
99         disconnect(transactioninterface,
100                 SIGNAL (RequireRestart(const QString&, const QString&)),
101                 this,
102                 SLOT(proxy_RequireRestart(const QString&, const QString &))
103         );
104
105
106         disconnect(transactioninterface,
107                 SIGNAL (Message(const QString&, const QString&)),
108                 this,
109                 SLOT(proxy_Message(const QString&, const QString &))
110         );
111
112
113
114
115         transactioninterface->deleteLater();
116
117 }
118
119 void QPackageKitConnector::slotError(const QString& code, const QString& message)
120 {
121         emit ErrorMessage("PackageKit Error " + code + ": " + message);  
122 }
123
124 QPackageKitTransactionInterface* QPackageKitConnector::connectToInterface()
125 {
126
127         QString tid = getTid();
128         if (tid == QString())   // tid is not valid
129         {
130                 emit ErrorMessage( 
131                         "DBus error : Unable to get transaction id  from packagekit. DBUS/PackageKit not running? "
132                                  );
133                 return NULL;
134         }
135
136         QPackageKitTransactionInterface *transactioninterface = new QPackageKitTransactionInterface(QString::fromAscii(PK_DBUS_SERVICE),
137                                                                                               tid,
138                                                                                               QDBusConnection::systemBus() );
139
140         connect(transactioninterface, 
141                 SIGNAL( Package(const QString, const QString, const QString) ), 
142                 this,
143                 SLOT( proxy_Package(const QString, const QString, const QString) )
144         );
145
146         connect(transactioninterface,
147           SIGNAL( Finished(const QString, uint ) ),
148           this,
149           SLOT( proxy_Finished(const QString, uint ) )
150         );
151
152         connect(transactioninterface,
153              SIGNAL( Transaction(const QString &, const QString &, bool, const QString &, uint, const QString &) ),
154              SLOT(proxy_Transaction(const QString &, const QString &, bool, const QString &, uint, const QString &) )
155         );
156   
157         connect(transactioninterface,
158                 SIGNAL( ProgressChanged(uint, uint, uint, uint) ),
159                 this,
160                 SLOT( proxy_ProgressChanged(uint,uint, uint, uint) )
161         );
162
163         connect(transactioninterface,
164                 SIGNAL( StatusChanged(const QString &) ),
165                 this,
166                 SLOT( proxy_StatusChanged(const QString &) )
167         );
168
169
170
171
172         connect(transactioninterface, 
173                 SIGNAL( Details(const QString&, const QString&, const QString&,
174                                  const QString&, const QString&, qulonglong)),
175                 this,
176                 SLOT( proxy_Details(const QString&, const QString&, const QString&,
177                                 const QString&, const QString&, qulonglong))
178         );
179
180         connect (transactioninterface,
181                 SIGNAL( UpdateDetail(const QString&, const QString&, const QString&, const QString&, 
182                         const QString&, const QString&, const QString&, const QString&,
183                         const QString&, const QString&, const QString&, const QString&)),
184
185                 this,
186                 SLOT( proxy_UpdateDetail(const QString&, const QString&, const QString&, const QString&, 
187                         const QString&, const QString&, const QString&, const QString&,
188                         const QString&, const QString&, const QString&, const QString&))
189         );
190
191         connect(transactioninterface, 
192                 SIGNAL( ErrorCode(const QString&, const QString& )),
193                 this,
194                 SLOT( slotError( const QString&, const QString&))
195         );
196
197         connect(transactioninterface,
198                 SIGNAL ( RepoSignatureRequired(const QString&,  const QString&,
199                         const QString&, const QString&, const QString&,
200                         const QString&, const QString&, const QString&)),
201                 this,
202                 SLOT (proxy_RepoSignatureRequired(const QString&, const QString&,
203                         const QString&, const QString&, const QString &,
204                         const QString&, const QString&, const QString &))
205         );
206
207
208         connect(transactioninterface,
209                 SIGNAL( EulaRequired(const QString&, const QString&, const QString&,
210                                 const QString&)),
211                 this,
212                 SLOT( proxy_EulaRequired(const QString&, const QString&, const QString&,
213                                 const QString&))
214         );
215
216         connect(transactioninterface,
217                 SIGNAL (RepoDetail(const QString&, const QString&, bool)),
218                 this,
219                 SLOT(proxy_RepoDetail(const QString&, const QString &, bool))
220         );
221
222         connect(transactioninterface,
223                 SIGNAL (RequireRestart(const QString&, const QString&)),
224                 this,
225                 SLOT(proxy_RequireRestart(const QString&, const QString &))
226         );
227
228         connect(transactioninterface,
229                 SIGNAL (Message(const QString&, const QString&)),
230                 this,
231                 SLOT(proxy_Message(const QString&, const QString &))
232         );
233
234
235         return transactioninterface;
236
237 }
238
239
240
241 void QPackageKitConnector::sendErrorMessage(QDBusAbstractInterface* interface)
242 {
243         emit ErrorMessage( "DBus error : " 
244                 + interface->lastError().name() +  " : " 
245                 + interface->lastError().message() );
246
247
248 }
249
250
251 bool QPackageKitConnector::getDetails(QStringList packageId)
252 {
253         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
254
255         if (!transactioninterface)
256                 return false;
257
258         QDBusReply<void> reply = transactioninterface->GetDetails(packageId);
259         if(reply.isValid())
260         {
261                 return true;
262         }
263         else
264         {
265                 sendErrorMessage(transactioninterface);
266                 return false;
267         }
268 }
269
270
271
272 bool QPackageKitConnector::getUpdateDetail(QStringList packageId)
273 {
274         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
275
276         if (!transactioninterface)
277                 return false;
278
279         QDBusReply<void> reply = transactioninterface->GetUpdateDetail(packageId);
280         if(reply.isValid())
281         {
282                 return true;
283         }
284         else
285         {
286                 sendErrorMessage(transactioninterface);
287                 return false;
288         }
289 }
290
291
292 bool QPackageKitConnector::acceptEula(QString eulaId)
293 {
294         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
295
296         if (!transactioninterface)
297                 return false;
298
299         QDBusReply<void> reply = transactioninterface->AcceptEula(eulaId);
300         if(reply.isValid())
301         {
302                 return true;
303         }
304         else
305         {
306                 sendErrorMessage(transactioninterface);
307                 return false;
308         }
309 }
310
311 bool QPackageKitConnector::getUpdates()
312 {
313         _lastAction    = QString("getUpdates");
314         _lastActionArg = false;
315
316         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
317
318         if (!transactioninterface)
319                 return false;
320         QDBusReply<void> reply = transactioninterface->GetUpdates("none");      // TODO 
321         if(reply.isValid())
322         {
323                 kDebug() << "updates replay valid";
324                 return true;
325         }
326         else
327         {
328                 kDebug() << "updates replay not valid";
329                 sendErrorMessage(transactioninterface);
330                 return false;
331         }
332 }
333
334 bool QPackageKitConnector::getDrivers()
335 {
336         _lastAction    = QString("getDrivers");
337         _lastActionArg = false;
338
339         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
340
341         if (!transactioninterface)
342                 return false;
343         QDBusReply<void> reply = transactioninterface->WhatProvides("none", "any", "drivers_for_attached_hardware");
344
345         if(reply.isValid())
346         {
347                 kDebug() << "drivers replay valid";
348                 return true;
349         }
350         else
351         {
352                 kDebug() << "drivers replay not valid";
353                 sendErrorMessage(transactioninterface);
354                 return false;
355         }
356 }
357
358
359 bool QPackageKitConnector::getRepoList()
360 {
361         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
362
363         if (!transactioninterface)
364                 return false;
365         QDBusReply<void> reply = transactioninterface->GetRepoList("none");
366         if(reply.isValid())
367         {
368                 kDebug() << "get repolist valid";
369                 return true;
370         }
371         else
372         {
373                 kDebug() << "get repolist not valid";
374                 sendErrorMessage(transactioninterface);
375                 return false;
376         }
377 }
378
379
380 bool QPackageKitConnector::refreshCache(bool force)
381 {
382         _lastAction    = QString("refreshCache");
383         _lastActionArg = force;
384
385         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
386
387         if (!transactioninterface)
388                 return false;
389         QDBusReply<void> reply = transactioninterface->RefreshCache(force);
390         if(reply.isValid())
391         {
392                 kDebug() << "refresh replay valid";
393                 return true;
394         }
395         else
396         {
397                 kDebug() << "refresh replay not valid";
398                 sendErrorMessage(transactioninterface);
399                 return false;
400         }
401 }
402
403 QString QPackageKitConnector::getTid()
404 {
405     QPackageKitInterface *interface = new QPackageKitInterface( QString::fromAscii(PK_DBUS_SERVICE),
406                                                                 QString::fromAscii(PK_DBUS_PATH),
407                                                                 QDBusConnection::systemBus() );
408
409         QDBusReply<QString> tid = interface->GetTid();
410         if(tid.isValid())
411         {
412                 kDebug() << "tid: " << tid;
413                 return tid;
414         }
415         else
416         {
417                 sendErrorMessage(interface);
418                 return QString();
419         }
420         
421         delete interface;
422
423 }
424
425 bool QPackageKitConnector::triggerLastActionAgain()
426 {
427         kDebug() << "Starting last action again..."; 
428
429         if (_lastAction == QString("getUpdates"))
430                 return getUpdates();
431         else if (_lastAction == QString("getDrivers"))
432                 return getDrivers();
433         else if (_lastAction    == QString("refreshCache"))
434                 return refreshCache(_lastActionArg.toBool());
435         else if (_lastAction    == QString("installPackages"))
436                 return installPackages(_lastActionArg.toStringList());
437         else if (_lastAction    == QString("updatePackages"))
438                 return updatePackages(_lastActionArg.toStringList());
439         else 
440                 return false;
441 }
442
443 bool QPackageKitConnector::installSignature(QString type, QString key_id, QString package_id)
444 {
445         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
446
447         if (!transactioninterface)
448                 return false;
449
450         QDBusReply<void> reply = transactioninterface->InstallSignature(type, key_id, package_id);
451         if(reply.isValid())
452         {
453
454                 kDebug() << "install sig valid";
455                 return true;
456         }
457         else
458         {
459                 kDebug() << "install sig not valid";
460                 sendErrorMessage(transactioninterface);
461                 return false;
462         }
463
464 }
465
466 bool QPackageKitConnector::installPackages(QStringList packageIds)
467 {
468         _lastAction    = QString("installPackages");
469         _lastActionArg = packageIds;
470
471         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
472
473         if (!transactioninterface)
474                 return false;
475
476         QDBusReply<void> reply = transactioninterface->InstallPackages(packageIds);
477         if(reply.isValid())
478         {
479
480                 kDebug() << "install valid";
481                 return true;
482         }
483         else
484         {
485                 kDebug() << "install not valid";
486                 sendErrorMessage(transactioninterface);
487                 return false;
488         }
489 }
490
491 bool QPackageKitConnector::updatePackages(QStringList packageIds)
492 {
493         _lastAction    = QString("updatePackages");
494         _lastActionArg = packageIds;
495
496
497         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
498
499         if (!transactioninterface)
500                 return false;
501
502         QDBusReply<void> reply = transactioninterface->UpdatePackages(packageIds);
503         if(reply.isValid())
504         {
505
506                 kDebug() << "updatepackages valid";
507                 return true;
508         }
509         else
510         {
511                 kDebug() << "updatepackages not valid";
512                 sendErrorMessage(transactioninterface);
513                 return false;
514         }
515 }
516
517
518
519 void QPackageKitConnector::proxy_Package( const QString &info, const QString &package_id, const QString &summary)
520 {
521         emit Package(info, package_id, summary);
522 }
523
524 void QPackageKitConnector::proxy_Finished(const QString &status, uint runtime)
525 {
526         kDebug() << "Finished";
527
528         QPackageKitTransactionInterface *transactioninterface = qobject_cast<QPackageKitTransactionInterface *>(QObject::sender());
529   
530         if ( transactioninterface )
531         {
532                 QString param;
533                 QDBusReply<QString> reply = transactioninterface->GetRole(param);
534                 if(reply.isValid())
535                 {
536                         kDebug() << "updates replay valid: " << param << " " << reply.value();
537                 }
538                 else
539                 {
540                         kDebug() << "role replay not valid";
541                         sendErrorMessage(transactioninterface);
542                         return;
543                 }
544
545                 if ( reply.value() == "get-updates" )
546                         emit UpdatesFinished();
547
548                 if ( reply.value() == "what-provides" )
549                         emit UpdatesFinished();
550
551
552
553                 kDebug() <<  reply.value() ;
554
555                 disconnectFromInterface(transactioninterface);
556                 emit Finished(Exit::fromString(status), runtime);
557         } 
558         else
559         {
560                 kDebug() << "Can't figure out which transaction is finishing";
561         }
562 }
563
564 void QPackageKitConnector::proxy_Transaction(const QString &old_tid, const QString &timespec, bool succeeded, const QString &role, uint duration, const QString &data)
565 {
566         kDebug() << "Transaction: " << old_tid << " " << role;
567 }
568
569
570 void QPackageKitConnector::proxy_ProgressChanged(uint percentage, uint subpercentage, uint elapsed, uint remaining)
571 {
572         if(percentage == 101)
573         {
574                 emit NoProgressUpdates();
575         }
576         else
577         {
578                 emit ProgressChanged(percentage, subpercentage, elapsed, remaining);
579         }
580 }
581
582 void QPackageKitConnector::proxy_StatusChanged(const QString &status)
583 {
584     kDebug() << "status changed to: " << status;
585     emit StatusChanged(status);
586 }
587
588
589 void QPackageKitConnector::proxy_RepoSignatureRequired(const QString &package_id, const QString &repository_name,
590          const QString  &key_url, const QString &key_userid, const QString &key_id,
591          const QString &key_fingerprint, const QString &key_timestamp, const QString &type)
592 {
593         kDebug() << "proxy_RepoSignatureRequired";
594
595         emit RepoSignatureRequired(package_id,  repository_name, key_url, key_userid, 
596                 key_id, key_fingerprint, key_timestamp, type); 
597 }
598
599
600 void QPackageKitConnector::proxy_Details(const QString &package_id, const QString &licence, const QString &group, const QString &detail,
601         const QString &url, qulonglong size) 
602 {
603         emit Details(package_id, licence, group, detail, url, size); 
604 }
605
606 void QPackageKitConnector::proxy_EulaRequired(const QString &eula_id, const QString &package_id, const QString &vendor_name, const QString &license_agreement)
607 {
608         emit EulaRequired(eula_id, package_id, vendor_name, license_agreement);
609 }       
610
611
612 void QPackageKitConnector::proxy_RepoDetail(const QString &repo_id, const QString &desc, bool enabled)
613 {
614         emit RepoDetail(repo_id, desc, enabled);
615 }
616
617 void QPackageKitConnector::proxy_UpdateDetail(const QString &package_id, const QString &updates, const QString &obsoletes, const QString &vendor_url,
618         const QString &bugzilla_url, const QString &cve_url, const QString &restart, const QString &update_text,
619         const QString &changelog, const QString &state, const QString &issued, const QString &updated)
620 {
621         emit UpdateDetail(package_id, updates, obsoletes, vendor_url, bugzilla_url, cve_url, restart, update_text,
622                 changelog, state, issued, updated);
623 }
624
625 void QPackageKitConnector::proxy_RequireRestart(const QString &type, const QString &details)
626 {
627         emit RequireRestart(type, details);
628 }
629
630 void QPackageKitConnector::proxy_Message(const QString &type, const QString &details)
631 {
632         emit Message(type, details);
633 }
634