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