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