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