cleanup
[fapman] / mainwindow.cpp
1 /*
2         This file is part of Faster Application Manager.
3
4         Faster Application Manager is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 3 of the License, or
7         (at your option) any later version.
8
9         Faster Application Manager is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with Faster Application Manager.  If not, see <http://www.gnu.org/licenses/>.
16
17         (C) Heikki Holstila 2010
18 */
19
20 #ifdef MYDEF_GTK_EXISTS
21 #include <gdk/gdk.h>
22 #include <gtk/gtk.h>
23 #endif
24
25 #include <QtCore>
26 #include <QtGui>
27 #include <QDBusConnection>
28 #include <QDBusInterface>
29 #include <phonon/AudioOutput>
30 #include <phonon/MediaObject>
31
32 #ifdef Q_WS_MAEMO_5
33 #include <QtMaemo5>
34 #endif
35
36 #include "mainwindow.h"
37 #include "version.h"
38 #include "ui_mainwindow.h"
39 #include "aaptinterface.h"
40 #include "packageview.h"
41 #include "confirmdialog.h"
42 #include "dimmer.h"
43 #include "repoview.h"
44 #include "help.h"
45 #include "settings.h"
46 #include "logview.h"
47 #include "rotatingbackground.h"
48 #include "dpkginterface.h"
49
50
51 MainWindow::MainWindow(QWidget *parent) :
52     QMainWindow(parent),
53     ui(new Ui::MainWindow)
54 {
55     ui->setupUi(this);
56
57         iAptInterface = new AAptInterface(this);
58         iWinPackageView = new PackageView(this);
59         iWinPackageView->setAptInterface(iAptInterface);
60         iWinRepoView = new RepoView(this);
61         iWinRepoView->setAptInterface(iAptInterface);
62         iSettings = new Settings(this);
63         iSettings->setAptInterface(iAptInterface);
64         iSettings->setPackageView(iWinPackageView);
65         iWinPackageView->setSettings(iSettings);
66         iAptInterface->setSettings(iSettings);
67         iDpkgInterface = new DpkgInterface(this);
68
69         iWinPackageView->setSortOrder( (PackageView::sortOrder)iSettings->qsettings()->value("default_sort_order",0).toInt() );
70
71         iWinPackageView->setSearchOptions( iSettings->qsettings()->value("search_pkgnames",true).toBool(),
72                                                                            iSettings->qsettings()->value("search_displaynames",true).toBool(),
73                                                                            iSettings->qsettings()->value("search_descshort",true).toBool(),
74                                                                            iSettings->qsettings()->value("search_desclong",false).toBool() );
75
76 #ifdef Q_WS_MAEMO_5
77         this->setAttribute(Qt::WA_Maemo5StackedWindow);
78         if( !iSettings->qsettings()->value("disable_autorotation",false).toBool() ) {
79                 this->setAttribute(Qt::WA_Maemo5AutoOrientation);
80         } else {
81                 this->setAttribute(Qt::WA_Maemo5LandscapeOrientation);
82         }
83 #endif
84
85         iDimmer = new dimmer(this);
86
87         iReposAutoUpdating = false;
88         iUpgradeAutoUpdate = true;
89         iNextOperation = OpNone;
90
91         ui->centralWidget->loadWallpaper();
92
93         QString stylesheet_mainscreen =
94                         "QPushButton {"
95                         "border-radius: 16px;"
96                         "border-width: 1px;"
97                         "border-color: palette(light);"
98                         "border-style: outset;"
99                         "padding-right: 10px;"
100                         "padding-left: 10px;"
101                         "color: palette(buttontext);"
102                         "background: rgba(255,255,255,80);"
103                         "}"
104                         "QPushButton:pressed {"
105                         "border-style: inset;"
106                         "background-color: rgba(255,255,255,150);"
107                         "}";
108
109         if( ((QApplication*)QApplication::instance())->styleSheet().isEmpty() )
110         {
111                 QString stylesheet_file;
112                 QFile f("/root/.fapman/style.css");
113                 if( f.open(QIODevice::ReadOnly | QIODevice::Text ) )
114                 {
115                         while(!f.atEnd()) {
116                                 stylesheet_file += f.readLine().trimmed();
117                         }
118                         f.close();
119                 }
120
121                 if( stylesheet_file.isEmpty() ) {
122                         ui->centralWidget->setStyleSheet(stylesheet_mainscreen);
123                 } else {
124                         ((QApplication*)QApplication::instance())->setStyleSheet(stylesheet_file);
125                 }
126         }
127
128
129         /*
130         // does not work
131
132         QDBusConnection conn = QDBusConnection::connectToBus(QDBusConnection::SystemBus, "faster_application_manager");
133
134         QString service = "com.nokia.icd";
135         QString path = "/com/nokia/icd";
136         QString method = "connect";
137
138         QDBusInterface net(service, path, service, conn, this);
139         net.call(method,"[ANY]",0);
140         */
141
142         iMediaObject = new Phonon::MediaObject(this);
143         Phonon::AudioOutput* aout = new Phonon::AudioOutput(Phonon::NotificationCategory, this);
144         Phonon::createPath(iMediaObject, aout);
145
146         resetIdlingTime();
147 }
148
149 MainWindow::~MainWindow()
150 {
151         // save "need repo refresh" status
152         iSettings->qsettings()->setValue("need_repo_refresh", iAptInterface->needRepoRefresh());
153
154         delete iWinPackageView;
155         delete iWinRepoView;
156         delete iAptInterface;
157         delete iDpkgInterface;
158         delete iDimmer;
159         delete iSettings;
160     delete ui;
161 }
162
163 void MainWindow::changeEvent(QEvent *e)
164 {
165     QMainWindow::changeEvent(e);
166     switch (e->type()) {
167     case QEvent::LanguageChange:
168         ui->retranslateUi(this);
169         break;
170     default:
171         break;
172     }
173 }
174
175 void MainWindow::on_btnRepos_clicked()
176 {
177         iWinRepoView->openWin();
178 }
179
180 void MainWindow::on_btnUpdate_clicked()
181 {       
182         // update catalogs
183
184         busyDialog(true, tr("Operation in progress"), tr("Updating catalogs"));
185
186         iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetUpdate);
187         iAptInterface->run(iDimmer);
188 }
189
190 #ifdef Q_WS_MAEMO_5
191 int MainWindow::top_application()
192 {
193         show();
194         activateWindow();
195         raise();
196         return 0;
197 }
198 #endif
199
200 void MainWindow::dateFetchAsk()
201 {
202         if( !iSettings->qsettings()->value("firstrun_asked_fetch_dates",false).toBool() &&
203                 !iSettings->qsettings()->value("fetch_dates",false).toBool() )
204         {
205                 iSettings->qsettings()->setValue("firstrun_asked_fetch_dates", true);
206                 ConfirmDialog d(true, this);
207                 d.setText("Fetch date information?","Enable date information fetching for packages? You have to enable it in order to be "
208                                   "able to sort packages by date. The first fetch can be slow but the dates are cached for later use. You can later "
209                                   "change this selection from the options menu.");
210                 if( d.exec() )
211                         iSettings->qsettings()->setValue("fetch_dates", true);
212         }
213         if( iSettings->qsettings()->value("fetch_dates",false).toBool() )
214         {
215                 iSettings->qsettings()->setValue("firstrun_asked_fetch_dates", true);   // don't ask if the option has already been enabled
216                 if( !iAptInterface->dateCacheExists() )
217                 {
218                         ConfirmDialog d(false, this);
219                         d.setText("Notice","Date information will be fetched only for packages from maemo.org and only for user categories.");
220                         d.exec();
221                 }
222                 iAptInterface->addQueuedOperation(AAptInterface::ModeFetchDates);
223         }
224 }
225
226 void MainWindow::on_btnListInstallable_clicked()
227 {
228         //install
229
230         if( iIdlingSince < QDateTime::currentDateTime().addSecs(-30*60) )
231                 iAptInterface->setNeedRefresh(-1,1,1,1);
232
233         iWinPackageView->setStatFilter( Package::PkgStatNotInstalled );
234
235         if( iAptInterface->needRepoRefresh() )
236                 iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetUpdate);
237
238         busyDialog(true, tr("Operation in progress"), tr("Reading package lists"));
239
240         iNextOperation = OpOpenPkgView;
241         iAptInterface->addQueuedOperation(AAptInterface::ModeReadPackages);
242
243         dateFetchAsk();
244
245         iAptInterface->run(iDimmer);
246 }
247
248 void MainWindow::on_btnUpgrade_clicked()
249 {
250         // upgrade
251
252         if( iIdlingSince < QDateTime::currentDateTime().addSecs(-30*60) )
253                 iAptInterface->setNeedRefresh(-1,1,1,1);
254
255         iWinPackageView->setStatFilter( Package::PkgStatUpgradeable );
256
257         if( iAptInterface->needRepoRefresh() )
258                 iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetUpdate);
259
260         busyDialog(true, tr("Operation in progress"), tr("Reading package lists"));
261
262         iNextOperation = OpOpenPkgView;
263         iAptInterface->addQueuedOperation(AAptInterface::ModeReadPackages);
264
265         dateFetchAsk();
266
267         iAptInterface->run(iDimmer);
268 }
269
270 void MainWindow::on_btnListInstalled_clicked()
271 {
272         //remove
273
274         if( !iSettings->qsettings()->value("remove_readfull",false).toBool() )
275                 iAptInterface->setSkipListAndDates();
276
277         if( iIdlingSince < QDateTime::currentDateTime().addSecs(-30*60) )
278                 iAptInterface->setNeedRefresh(-1,-1,1,-1);
279
280         iWinPackageView->setStatFilter( Package::PkgStatInstalled );
281
282         busyDialog(true, tr("Operation in progress"), tr("Reading package lists"));
283
284         iNextOperation = OpOpenPkgView;
285         iAptInterface->addQueuedOperation(AAptInterface::ModeReadPackages);
286
287         dateFetchAsk();
288
289         iAptInterface->run(iDimmer);
290 }
291
292 void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> lastModes, bool success, QString title, QStringList msgs)
293 {
294         if( lastModes.contains(AAptInterface::ModeAptGetSimulate) && success ) {
295                 iNextOperation = OpPromptSimulated;
296         }
297         if( lastModes.contains(AAptInterface::ModeAptGetInstall) && success ) {
298                 iWinPackageView->clearSelections();
299                 busyDialog(false);
300                 iWinPackageView->close();
301                 iNextOperation = OpNone;
302
303                 GdkEventIconThemeReload();
304         }
305
306         if( iNextOperation == OpOpenPkgView && success )
307         {
308                 iWinPackageView->openWin();
309                 busyDialog(false);
310                 iNextOperation = OpNone;
311         } else if( iNextOperation == OpPromptSimulated && success )
312         {
313                 QStringList inst,remv,instver,remvver;
314                 QStringList all = iAptInterface->processPackages();
315                 QStringList vers = iAptInterface->processPackageVersions();
316                 for(int zyx=0; zyx<all.count(); zyx++)
317                 {
318                         if( all.at(zyx).endsWith('-') )
319                         {
320                                 remv.append( all.at(zyx).left( all.at(zyx).size()-1 ) );
321                                 if( vers.count()>zyx )
322                                         remvver.append( vers.at(zyx) );
323                         } else {
324                                 inst.append( all.at(zyx) );
325                                 if( vers.count()>zyx )
326                                         instver.append( vers.at(zyx) );
327                         }
328                 }
329
330                 int total_dl_size = 0;
331                 for( int i=0; i<inst.count(); i++ ) {
332                         Package* pkg = iAptInterface->packagesAvailable()->value(inst.at(i),0);
333                         if( pkg ) {
334                                 total_dl_size += pkg->size()/1024;
335                         }
336                 }
337
338                 QString pkglist;
339                 pkglist = QString("<b>The following operations will be performed:</b><br>"
340                                   "%1 to install/upgrade, %2 to remove<br>").arg(inst.count()).arg(remv.count());
341                 if( inst.count()>0 )
342                         pkglist += QString("Total download size: %L1 kB<br>").arg(total_dl_size);
343                 bool mismatch = false;
344
345                 if( remv.count()>0 ) {
346                         pkglist += "<br><b><u>REMOVE:</u></b><br><font size=\"-1\">";
347                         for( int i=0; i<remv.count(); i++ ) {
348                                 pkglist += "<b>" + remv.at(i) + "</b>";
349                                 Package* pkg = iAptInterface->packagesInstalled()->value(remv.at(i),0);
350                                 if( !pkg ) {
351                                         qDebug() << "Warning: unknown package" << remv.at(i);
352                                         pkglist += "<font color=\"red\">***UNKNOWN***</font>";
353                                         mismatch = true;
354                                 }
355                                 if( remvver.count()>i ) {
356                                         pkglist += " " + remvver.at(i);
357                                         if( pkg && remvver.at(i) != pkg->version() ) {
358                                                 qDebug() << "Version mismatch, database version is" << pkg->version() << "but removing" << remvver.at(i);
359                                                 mismatch = true;
360                                                 pkglist += " <font color=\"red\">***TRYING TO REMOVE " + pkg->version() + "***</font> ";
361                                         }
362                                 }
363                                 if( pkg && pkg->installedSize()>0 )
364                                         pkglist += QString(" (%L1 kB)").arg(pkg->installedSize());
365                                 pkglist += "<br>";
366                         }
367                 }
368                 pkglist += "</font>";
369
370                 bool installing_blacklisted = false;
371                 if( inst.count()>0 ) {
372                         pkglist += "<br><b><u>INSTALL/UPGRADE:</u></b><br><font size=\"-1\">";
373                         for( int i=0; i<inst.count(); i++ ) {
374                                 pkglist += "<b>" + inst.at(i) + "</b>";
375                                 Package* pkg = iAptInterface->packagesAvailable()->value(inst.at(i),0);
376                                 if( !pkg ) {
377                                         qDebug() << "Warning: unknown package" << inst.at(i);
378                                         pkglist += "<font color=\"red\">***NEW/UNKNOWN***</font>";
379                                         mismatch = true;
380                                 }
381                                 if( pkg && pkg->isBlacklisted() ) {
382                                         qDebug() << "Warning: installing blacklisted package" << inst.at(i);
383                                         pkglist += "<font color=\"red\">***BLACKLISTED***</font>";
384                                         installing_blacklisted = true;
385                                 }
386                                 if( instver.count()>i ) {
387                                         pkglist += " " + instver.at(i);
388                                         if( pkg && instver.at(i) != pkg->version() ) {
389                                                 qDebug() << "Version mismatch, database version is" << pkg->version() << "but installing" << instver.at(i);
390                                                 mismatch = true;
391                                                 pkglist += " <font color=\"red\">***TRYING TO INSTALL " + pkg->version() + "***</font> ";
392                                         }
393                                 }
394                                 if( pkg && pkg->size()>0 ) {
395                                         pkglist += QString(" (%L1 kB)").arg(pkg->size()/1024);
396                                 }
397                                 pkglist += "<br>";
398                         }
399                 }
400                 pkglist += "</font>";
401
402                 if( mismatch ) {
403                         ConfirmDialog m(false, this);
404                         m.setText("Warning", "There is a version mismatch between your original package selections and some of the packages being installed " \
405                                           "from the repositories. This could be due to your application catalogs being out of date.");
406                         m.exec();
407                 }
408                 if( installing_blacklisted ) {
409                         ConfirmDialog b(false, this);
410                         b.setText("Warning","Blacklisted package(s) will be installed");
411                         b.exec();
412                 }
413
414                 busyDialog(false);
415                 ConfirmDialog d(true, this);
416                 d.setText("Confirmation",pkglist);
417                 if( d.exec() ) {
418                         iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetInstall);
419
420                         if( iSettings->qsettings()->value("enable_autoclean",true).toBool() && inst.count()>0 )
421                                 iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetClean);
422
423                         QString busytext;
424                         if( remv.count() > 0 )
425                                 busytext += QString("Remove %1 package(s)<br>").arg(remv.count());
426                         if( inst.count() > 0 )
427                                 busytext += QString("Install %1 package(s)<br>").arg(inst.count());
428                         busytext += "Preparing...";
429                         busyDialog(true, "Operation in progress", busytext);
430
431                         // "run" really does nothing here since the previous item should still be running
432                         if( iWinPackageView->isVisible() ) {
433                                 iAptInterface->run(iWinPackageView->mydimmer());
434                         } else {
435                                 iAptInterface->run(iDimmer);
436                         }
437                 }
438                 iNextOperation = OpNone;
439                 return;
440         } else {
441                 busyDialog(false);
442                 iNextOperation = OpNone;
443
444                 if( iSettings->qsettings()->value("sound_notify",false).toBool() )
445                 {
446                         qDebug() << "playing sound";
447                         iMediaObject->setCurrentSource( Phonon::MediaSource(iSettings->qsettings()->value("sound_file","/usr/share/sounds/ui-operation_ready.wav").toString()) );
448                         iMediaObject->play();
449                 }
450
451                 QString text = "<br><b><u>Faster Application Manager</u></b><br>"
452                                 "<b>"+title+"</b><br>" + msgs.join("<br>") + "<br>";
453
454 #ifdef Q_WS_MAEMO_5
455                 QMaemo5InformationBox::information(0, text, QMaemo5InformationBox::NoTimeout);
456 #endif
457         }
458
459 }
460
461 void MainWindow::busyDialog(bool show_, QString title, QString text)
462 {
463         if( show_ ) {
464                 iDimmer->setProgress(-1);
465                 ui->menuMenu->setEnabled(false);
466                 ui->centralWidget->setEnabled(false);
467                 iWinPackageView->disableMenu();
468                 iDimmer->resizeEvent(0);
469                 iDimmer->dim(title, text);
470                 iWinPackageView->mydimmer()->resizeEvent(0);
471                 iWinPackageView->mydimmer()->dim(title, text);
472         } else {
473                 iDimmer->undim();
474                 iWinPackageView->mydimmer()->undim();
475                 ui->menuMenu->setEnabled(true);
476                 ui->centralWidget->setEnabled(true);
477                 iWinPackageView->enableMenu();
478         }
479 }
480
481 void MainWindow::on_actionAbout_triggered()
482 {
483         ConfirmDialog d(false, this);
484         d.setText("About","Faster Application Manager<br>"
485                           "<font size=\"-1\">Version " + PROGRAM_VERSION + "</font><br><br>"
486                           "(C) Heikki Holstila 2010<br>Donate using "
487                           "<a href=\"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=6ZKRY5QFHL42A&lc=FI&item_name=Faster%20Application%20Manager"
488                           "%20for%20Maemo5&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted\">PayPal</a>");
489         d.exec();
490 }
491
492 void MainWindow::on_actionClean_triggered()
493 {
494         //if( iOperation != OpNone ) return;
495         //iOperation = OpClean;
496         iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetClean);
497         iAptInterface->run(iDimmer);
498 }
499
500 void MainWindow::closeEvent(QCloseEvent *event)
501 {
502         if( iDimmer->busy() ) {
503                 iAptInterface->cancel();
504                 event->ignore();
505         } else {
506                 event->accept();
507         }
508 }
509
510 void MainWindow::on_actionView_log_triggered()
511 {
512         QByteArray log = iAptInterface->readLogFile();
513         LogView l(log, this);
514         l.exec();
515 }
516
517 void MainWindow::on_actionOptions_triggered()
518 {
519         iSettings->openWin();
520 }
521
522 void MainWindow::notifyDialog(QString title, QString msg)
523 {
524         ConfirmDialog d(false, this);
525         d.setText(title, msg);
526         d.exec();
527 }
528
529 bool MainWindow::confirmDialog(QString title, QString msg)
530 {
531         ConfirmDialog d(true, this);
532         d.setText(title, msg);
533         return d.exec();
534 }
535
536 void MainWindow::GdkEventIconThemeReload()
537 {
538         // DOES NOT EVEN WORK (at least not reliably) - disabled from the project file
539
540 #ifdef MYDEF_GTK_EXISTS
541         qDebug() << "Sending GDK icon theme reload event";
542
543         gdk_init((int*)QApplication::argc(),(gchar***)QApplication::argv());
544
545         // taken from hildon application manager
546         GdkEventClient ev;
547         ev.type = GDK_CLIENT_EVENT;
548         ev.window = NULL;
549         ev.send_event = TRUE;
550         ev.message_type = gdk_atom_intern_static_string("_GTK_LOAD_ICONTHEMES");
551         ev.data_format = 32;
552         gdk_event_send_clientmessage_toall((GdkEvent*)&ev);
553
554         while(gdk_events_pending()) {
555                 g_main_context_iteration(NULL, true);
556         }
557
558 #endif
559 }
560
561 void MainWindow::on_actionLoad_file_triggered()
562 {
563         QStringList files = QFileDialog::getOpenFileNames(this, "Open files", "/", "Files (*.deb *.install)");
564         if( files.count() > 0 ) {
565                 QStringList debs = files.filter(QRegExp(".*\\.deb$"));
566                 QStringList installs = files.filter(QRegExp(".*\\.install$"));
567                 if( debs.count()>0 && installs.count()>0 ) {
568                         ConfirmDialog d(false, this);
569                         d.setText("Error", "You can't mix different file types in your selection");
570                         d.exec();
571                         return;
572                 }
573                 if( debs.count()>0 )
574                         iDpkgInterface->loadDebFiles(debs);
575                 else if( installs.count()>0 )
576                         iAptInterface->loadInstallFiles(installs);
577         }
578 }