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