stop packagekitd before calling yast
[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
404 bool QPackageKitConnector::suggestDaemonQuit()
405 {
406         QPackageKitInterface *interface = new QPackageKitInterface( QString::fromAscii(PK_DBUS_SERVICE),
407                                                                 QString::fromAscii(PK_DBUS_PATH),
408                                                                 QDBusConnection::systemBus() );
409
410         QDBusReply<void> reply = interface->SuggestDaemonQuit();
411         // we can't expect a valig reply, as packagekitd
412         // isn't able to answer when it doesn't run anymore :-)
413
414         return true;
415         delete interface;
416 }
417
418
419
420
421
422
423 QString QPackageKitConnector::getTid()
424 {
425     QPackageKitInterface *interface = new QPackageKitInterface( QString::fromAscii(PK_DBUS_SERVICE),
426                                                                 QString::fromAscii(PK_DBUS_PATH),
427                                                                 QDBusConnection::systemBus() );
428
429         QDBusReply<QString> tid = interface->GetTid();
430         if(tid.isValid())
431         {
432                 kDebug() << "tid: " << tid;
433                 return tid;
434         }
435         else
436         {
437                 sendErrorMessage(interface);
438                 return QString();
439         }
440         
441         delete interface;
442
443 }
444
445 bool QPackageKitConnector::triggerLastActionAgain()
446 {
447         kDebug() << "Starting last action again..."; 
448
449         if (_lastAction == QString("getUpdates"))
450                 return getUpdates();
451         else if (_lastAction == QString("getDrivers"))
452                 return getDrivers();
453         else if (_lastAction    == QString("refreshCache"))
454                 return refreshCache(_lastActionArg.toBool());
455         else if (_lastAction    == QString("installPackages"))
456                 return installPackages(_lastActionArg.toStringList());
457         else if (_lastAction    == QString("updatePackages"))
458                 return updatePackages(_lastActionArg.toStringList());
459         else 
460                 return false;
461 }
462
463 bool QPackageKitConnector::installSignature(QString type, QString key_id, QString package_id)
464 {
465         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
466
467         if (!transactioninterface)
468                 return false;
469
470         QDBusReply<void> reply = transactioninterface->InstallSignature(type, key_id, package_id);
471         if(reply.isValid())
472         {
473
474                 kDebug() << "install sig valid";
475                 return true;
476         }
477         else
478         {
479                 kDebug() << "install sig not valid";
480                 sendErrorMessage(transactioninterface);
481                 return false;
482         }
483
484 }
485
486 bool QPackageKitConnector::installPackages(QStringList packageIds)
487 {
488         _lastAction    = QString("installPackages");
489         _lastActionArg = packageIds;
490
491         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
492
493         if (!transactioninterface)
494                 return false;
495
496         QDBusReply<void> reply = transactioninterface->InstallPackages(packageIds);
497         if(reply.isValid())
498         {
499
500                 kDebug() << "install valid";
501                 return true;
502         }
503         else
504         {
505                 kDebug() << "install not valid";
506                 sendErrorMessage(transactioninterface);
507                 return false;
508         }
509 }
510
511 bool QPackageKitConnector::updatePackages(QStringList packageIds)
512 {
513         _lastAction    = QString("updatePackages");
514         _lastActionArg = packageIds;
515
516
517         QPackageKitTransactionInterface* transactioninterface = connectToInterface();
518
519         if (!transactioninterface)
520                 return false;
521
522         QDBusReply<void> reply = transactioninterface->UpdatePackages(packageIds);
523         if(reply.isValid())
524         {
525
526                 kDebug() << "updatepackages valid";
527                 return true;
528         }
529         else
530         {
531                 kDebug() << "updatepackages not valid";
532                 sendErrorMessage(transactioninterface);
533                 return false;
534         }
535 }
536
537
538
539 void QPackageKitConnector::proxy_Package( const QString &info, const QString &package_id, const QString &summary)
540 {
541         emit Package(info, package_id, summary);
542 }
543
544 void QPackageKitConnector::proxy_Finished(const QString &status, uint runtime)
545 {
546         kDebug() << "Finished";
547
548         QPackageKitTransactionInterface *transactioninterface = qobject_cast<QPackageKitTransactionInterface *>(QObject::sender());
549   
550         if ( transactioninterface )
551         {
552                 QString param;
553                 QDBusReply<QString> reply = transactioninterface->GetRole(param);
554                 if(reply.isValid())
555                 {
556                         kDebug() << "updates replay valid: " << param << " " << reply.value();
557                 }
558                 else
559                 {
560                         kDebug() << "role replay not valid";
561                         sendErrorMessage(transactioninterface);
562                         return;
563                 }
564
565                 if ( reply.value() == "get-updates" )
566                         emit UpdatesFinished();
567
568                 if ( reply.value() == "what-provides" )
569                         emit UpdatesFinished();
570
571
572
573                 kDebug() <<  reply.value() ;
574
575                 disconnectFromInterface(transactioninterface);
576                 emit Finished(Exit::fromString(status), runtime);
577         } 
578         else
579         {
580                 kDebug() << "Can't figure out which transaction is finishing";
581         }
582 }
583
584 void QPackageKitConnector::proxy_Transaction(const QString &old_tid, const QString &timespec, bool succeeded, const QString &role, uint duration, const QString &data)
585 {
586         Q_UNUSED(timespec);
587         Q_UNUSED(succeeded);
588         Q_UNUSED(duration);
589         Q_UNUSED(data);
590
591
592         kDebug() << "Transaction: " << old_tid << " " << role;
593 }
594
595
596 void QPackageKitConnector::proxy_ProgressChanged(uint percentage, uint subpercentage, uint elapsed, uint remaining)
597 {
598         if(percentage == 101)
599         {
600                 emit NoProgressUpdates();
601         }
602         else
603         {
604                 emit ProgressChanged(percentage, subpercentage, elapsed, remaining);
605         }
606 }
607
608 void QPackageKitConnector::proxy_StatusChanged(const QString &status)
609 {
610     kDebug() << "status changed to: " << status;
611     emit StatusChanged(status);
612 }
613
614
615 void QPackageKitConnector::proxy_RepoSignatureRequired(const QString &package_id, const QString &repository_name,
616          const QString  &key_url, const QString &key_userid, const QString &key_id,
617          const QString &key_fingerprint, const QString &key_timestamp, const QString &type)
618 {
619         kDebug() << "proxy_RepoSignatureRequired";
620
621         emit RepoSignatureRequired(package_id,  repository_name, key_url, key_userid, 
622                 key_id, key_fingerprint, key_timestamp, type); 
623 }
624
625
626 void QPackageKitConnector::proxy_Details(const QString &package_id, const QString &licence, const QString &group, const QString &detail,
627         const QString &url, qulonglong size) 
628 {
629         emit Details(package_id, licence, group, detail, url, size); 
630 }
631
632 void QPackageKitConnector::proxy_EulaRequired(const QString &eula_id, const QString &package_id, const QString &vendor_name, const QString &license_agreement)
633 {
634         emit EulaRequired(eula_id, package_id, vendor_name, license_agreement);
635 }       
636
637
638 void QPackageKitConnector::proxy_RepoDetail(const QString &repo_id, const QString &desc, bool enabled)
639 {
640         emit RepoDetail(repo_id, desc, enabled);
641 }
642
643 void QPackageKitConnector::proxy_UpdateDetail(const QString &package_id, const QString &updates, const QString &obsoletes, const QString &vendor_url,
644         const QString &bugzilla_url, const QString &cve_url, const QString &restart, const QString &update_text,
645         const QString &changelog, const QString &state, const QString &issued, const QString &updated)
646 {
647         emit UpdateDetail(package_id, updates, obsoletes, vendor_url, bugzilla_url, cve_url, restart, update_text,
648                 changelog, state, issued, updated);
649 }
650
651 void QPackageKitConnector::proxy_RequireRestart(const QString &type, const QString &details)
652 {
653         emit RequireRestart(type, details);
654 }
655
656 void QPackageKitConnector::proxy_Message(const QString &type, const QString &details)
657 {
658         emit Message(type, details);
659 }
660