ddebc48fdb17bc1a679f45e1d1b5d0d751f30521
[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
147 MainWindow::~MainWindow()
148 {
149         // save "need repo refresh" status
150         iSettings->qsettings()->setValue("need_repo_refresh", iAptInterface->needRepoRefresh());
151
152         delete iWinPackageView;
153         delete iWinRepoView;
154         delete iAptInterface;
155         delete iDpkgInterface;
156         delete iDimmer;
157         delete iSettings;
158     delete ui;
159 }
160
161 void MainWindow::changeEvent(QEvent *e)
162 {
163     QMainWindow::changeEvent(e);
164     switch (e->type()) {
165     case QEvent::LanguageChange:
166         ui->retranslateUi(this);
167         break;
168     default:
169         break;
170     }
171 }
172
173 void MainWindow::on_btnRepos_clicked()
174 {
175         iWinRepoView->openWin();
176 }
177
178 void MainWindow::on_btnUpdate_clicked()
179 {       
180         // update catalogs
181
182         busyDialog(true, tr("Operation in progress"), tr("Updating catalogs"));
183
184         iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetUpdate);
185         iAptInterface->run(iDimmer);
186 }
187
188 #ifdef Q_WS_MAEMO_5
189 int MainWindow::top_application()
190 {
191         show();
192         activateWindow();
193         raise();
194         return 0;
195 }
196 #endif
197
198 void MainWindow::dateFetchAsk()
199 {
200         if( !iSettings->qsettings()->value("firstrun_asked_fetch_dates",false).toBool() &&
201                 !iSettings->qsettings()->value("fetch_dates",false).toBool() )
202         {
203                 iSettings->qsettings()->setValue("firstrun_asked_fetch_dates", true);
204                 ConfirmDialog d(true, this);
205                 d.setText("Fetch date information?","Enable date information fetching for packages? You have to enable it in order to be "
206                                   "able to sort packages by date. The first fetch can be slow but the dates are cached for later use. You can later "
207                                   "change this selection from the options menu.");
208                 if( d.exec() )
209                         iSettings->qsettings()->setValue("fetch_dates", true);
210         }
211         if( iSettings->qsettings()->value("fetch_dates",false).toBool() )
212         {
213                 iSettings->qsettings()->setValue("firstrun_asked_fetch_dates", true);   // don't ask if the option has already been enabled
214                 if( !iAptInterface->dateCacheExists() )
215                 {
216                         ConfirmDialog d(false, this);
217                         d.setText("Notice","Date information will be fetched only for packages from maemo.org and only for user categories.");
218                         d.exec();
219                 }
220                 iAptInterface->addQueuedOperation(AAptInterface::ModeFetchDates);
221         }
222 }
223
224 void MainWindow::on_btnListInstallable_clicked()
225 {
226         //install
227
228         int listupd = -1;
229         int dpkgupd = -1;
230         if( iAptInterface->lastListUpdate() < QDateTime::currentDateTime().addSecs(-KListExpireTime) )
231                 listupd = 1;
232         if( iAptInterface->lastDpkgUpdate() < QDateTime::currentDateTime().addSecs(-KListExpireTime) )
233                 dpkgupd = 1;
234         iAptInterface->setNeedRefresh(-1,listupd,dpkgupd,listupd);
235
236         iWinPackageView->setStatFilter( Package::PkgStatNotInstalled );
237
238         if( iAptInterface->needRepoRefresh() && !iSettings->qsettings()->value("no_catalogs_autoupdate",false).toBool() )
239                 iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetUpdate);
240
241         busyDialog(true, tr("Operation in progress"), tr("Reading package lists"));
242
243         iNextOperation = OpOpenPkgView;
244         iAptInterface->addQueuedOperation(AAptInterface::ModeReadPackages);
245
246         dateFetchAsk();
247
248         iAptInterface->run(iDimmer);
249 }
250
251 void MainWindow::on_btnUpgrade_clicked()
252 {
253         // upgrade
254
255         int listupd = -1;
256         int dpkgupd = -1;
257         if( iAptInterface->lastListUpdate() < QDateTime::currentDateTime().addSecs(-KListExpireTime) )
258                 listupd = 1;
259         if( iAptInterface->lastDpkgUpdate() < QDateTime::currentDateTime().addSecs(-KListExpireTime) )
260                 dpkgupd = 1;
261         iAptInterface->setNeedRefresh(-1,listupd,dpkgupd,listupd);
262
263         iWinPackageView->setStatFilter( Package::PkgStatUpgradeable );
264
265         if( iAptInterface->needRepoRefresh() && !iSettings->qsettings()->value("no_catalogs_autoupdate",false).toBool() )
266                 iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetUpdate);
267
268         busyDialog(true, tr("Operation in progress"), tr("Reading package lists"));
269
270         iNextOperation = OpOpenPkgView;
271         iAptInterface->addQueuedOperation(AAptInterface::ModeReadPackages);
272
273         dateFetchAsk();
274
275         iAptInterface->run(iDimmer);
276 }
277
278 void MainWindow::on_btnListInstalled_clicked()
279 {
280         //remove
281
282         if( !iSettings->qsettings()->value("remove_readfull",false).toBool() )
283                 iAptInterface->setSkipListAndDates();
284
285         int dpkgupd = -1;
286         if( iAptInterface->lastDpkgUpdate() < QDateTime::currentDateTime().addSecs(-KListExpireTime) )
287                 dpkgupd = 1;
288         iAptInterface->setNeedRefresh(-1,-1,dpkgupd,-1);
289
290         iWinPackageView->setStatFilter( Package::PkgStatInstalled );
291
292         busyDialog(true, tr("Operation in progress"), tr("Reading package lists"));
293
294         iNextOperation = OpOpenPkgView;
295         iAptInterface->addQueuedOperation(AAptInterface::ModeReadPackages);
296
297         dateFetchAsk();
298
299         iAptInterface->run(iDimmer);
300 }
301
302 void MainWindow::operationQueueFinished(QList<AAptInterface::interfaceMode> lastModes, bool success, QString title, QStringList msgs)
303 {
304         if( lastModes.contains(AAptInterface::ModeAptGetSimulate) && success ) {
305                 iNextOperation = OpPromptSimulated;
306         }
307         if( lastModes.contains(AAptInterface::ModeAptGetInstall) && success ) {
308                 iWinPackageView->clearSelections();
309                 busyDialog(false);
310                 iWinPackageView->close();
311                 iNextOperation = OpNone;
312
313                 GdkEventIconThemeReload();
314         }
315
316         if( iNextOperation == OpOpenPkgView && success )
317         {
318                 iWinPackageView->openWin();
319                 busyDialog(false);
320                 iNextOperation = OpNone;
321         } else if( iNextOperation == OpPromptSimulated && success )
322         {
323                 QStringList inst,remv,instver,remvver;
324                 QStringList all = iAptInterface->processPackages();
325                 QStringList vers = iAptInterface->processPackageVersions();
326                 for(int zyx=0; zyx<all.count(); zyx++)
327                 {
328                         if( all.at(zyx).endsWith('-') )
329                         {
330                                 remv.append( all.at(zyx).left( all.at(zyx).size()-1 ) );
331                                 if( vers.count()>zyx )
332                                         remvver.append( vers.at(zyx) );
333                         } else {
334                                 inst.append( all.at(zyx) );
335                                 if( vers.count()>zyx )
336                                         instver.append( vers.at(zyx) );
337                         }
338                 }
339
340                 int total_dl_size = 0;
341                 for( int i=0; i<inst.count(); i++ ) {
342                         Package* pkg = iAptInterface->packagesAvailable()->value(inst.at(i),0);
343                         if( pkg ) {
344                                 total_dl_size += pkg->size()/1024;
345                         }
346                 }
347
348                 QString pkglist;
349                 pkglist = QString("<b>The following operations will be performed:</b><br>"
350                                   "%1 to install/upgrade, %2 to remove<br>").arg(inst.count()).arg(remv.count());
351                 if( inst.count()>0 )
352                         pkglist += QString("Total download size: %L1 kB<br>").arg(total_dl_size);
353                 bool mismatch = false;
354                 bool warn_system_package_remove = false;
355                 bool warn_system_package_install = false;
356
357                 if( remv.count()>0 ) {
358                         pkglist += "<br><b><u>REMOVE:</u></b><br><font size=\"-1\">";
359                         for( int i=0; i<remv.count(); i++ ) {
360                                 pkglist += "<b>" + remv.at(i) + "</b>";
361                                 Package* pkg = iAptInterface->packagesInstalled()->value(remv.at(i),0);
362                                 if( !pkg ) {
363                                         qDebug() << "Warning: unknown package" << remv.at(i);
364                                         pkglist += "<font color=\"red\">***UNKNOWN***</font>";
365                                         mismatch = true;
366                                 }
367 #ifdef Q_WS_MAEMO_5
368                                 if( pkg && pkg->maemoDisplayName()=="Maemo 5" ) {
369                                         warn_system_package_remove = true;
370                                 }
371 #endif
372                                 if( remvver.count()>i ) {
373                                         pkglist += " " + remvver.at(i);
374                                         if( pkg && remvver.at(i) != pkg->version() ) {
375                                                 qDebug() << "Version mismatch, database version is" << pkg->version() << "but removing" << remvver.at(i);
376                                                 mismatch = true;
377                                                 pkglist += " <font color=\"red\">***TRYING TO REMOVE " + pkg->version() + "***</font> ";
378                                         }
379                                 }
380                                 if( pkg && pkg->installedSize()>0 )
381                                         pkglist += QString(" (%L1 kB)").arg(pkg->installedSize());
382                                 pkglist += "<br>";
383                         }
384                 }
385                 pkglist += "</font>";
386
387                 bool installing_blacklisted = false;
388                 if( inst.count()>0 ) {
389                         pkglist += "<br><b><u>INSTALL/UPGRADE:</u></b><br><font size=\"-1\">";
390                         for( int i=0; i<inst.count(); i++ ) {
391                                 pkglist += "<b>" + inst.at(i) + "</b>";
392                                 Package* pkg = iAptInterface->packagesAvailable()->value(inst.at(i),0);
393                                 if( !pkg ) {
394                                         qDebug() << "Warning: unknown package" << inst.at(i);
395                                         pkglist += "<font color=\"red\">***NEW/UNKNOWN***</font>";
396                                         mismatch = true;
397                                 }
398                                 if( pkg && pkg->isBlacklisted() ) {
399                                         qDebug() << "Warning: installing blacklisted package" << inst.at(i);
400                                         pkglist += "<font color=\"red\">***BLACKLISTED***</font>";
401                                         installing_blacklisted = true;
402                                 }
403 #ifdef Q_WS_MAEMO_5
404                                 if( pkg && pkg->maemoDisplayName()=="Maemo 5" ) {
405                                         warn_system_package_install = true;
406                                 }
407 #endif
408                                 if( instver.count()>i ) {
409                                         pkglist += " " + instver.at(i);
410                                         if( pkg && instver.at(i) != pkg->version() ) {
411                                                 qDebug() << "Version mismatch, database version is" << pkg->version() << "but installing" << instver.at(i);
412                                                 mismatch = true;
413                                                 pkglist += " <font color=\"red\">***TRYING TO INSTALL " + pkg->version() + "***</font> ";
414                                         }
415                                 }
416                                 if( pkg && pkg->size()>0 ) {
417                                         pkglist += QString(" (%L1 kB)").arg(pkg->size()/1024);
418                                 }
419                                 pkglist += "<br>";
420                         }
421                 }
422                 pkglist += "</font>";
423
424                 if( mismatch ) {
425                         ConfirmDialog m(false, this);
426                         m.setText("Warning", "There is a version mismatch between your original package selections and some of the packages being installed " \
427                                           "from the repositories. This could be due to your application catalogs being out of date.");
428                         m.exec();
429                 }
430                 if( installing_blacklisted ) {
431                         ConfirmDialog b(false, this);
432                         b.setText("Warning","Blacklisted package(s) will be installed");
433                         b.exec();
434                 }
435                 if( warn_system_package_remove ) {
436                         ConfirmDialog s(false, this);
437                         s.setText("Warning","You are about to remove a critical system package.");
438                         s.exec();
439                 }
440                 if( warn_system_package_install ) {
441                         ConfirmDialog s(false, this);
442                         s.setText("Warning","You are trying to perform an install/upgrade operation on a critical system package. Doing a system upgrade with " \
443                                           "Faster Application Manager has not been tested and it could result in a horrible failure. You have been warned.");
444                         s.exec();
445                 }
446
447                 busyDialog(false);
448                 ConfirmDialog d(true, this);
449                 d.setText("Confirmation",pkglist);
450                 if( d.exec() ) {
451                         iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetInstall);
452
453                         if( iSettings->qsettings()->value("enable_autoclean",true).toBool() && inst.count()>0 )
454                                 iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetClean);
455
456                         QString busytext;
457                         if( remv.count() > 0 )
458                                 busytext += QString("Remove %1 package(s)<br>").arg(remv.count());
459                         if( inst.count() > 0 )
460                                 busytext += QString("Install %1 package(s)<br>").arg(inst.count());
461                         busytext += "Preparing...";
462                         busyDialog(true, "Operation in progress", busytext);
463
464                         // "run" really does nothing here since the previous item should still be running
465                         if( iWinPackageView->isVisible() ) {
466                                 iAptInterface->run(iWinPackageView->mydimmer());
467                         } else {
468                                 iAptInterface->run(iDimmer);
469                         }
470                 }
471                 iNextOperation = OpNone;
472                 return;
473         } else {
474                 busyDialog(false);
475                 iNextOperation = OpNone;
476
477                 if( iSettings->qsettings()->value("sound_notify",false).toBool() )
478                 {
479                         qDebug() << "playing sound";
480                         iMediaObject->setCurrentSource( Phonon::MediaSource(iSettings->qsettings()->value("sound_file","/usr/share/sounds/ui-operation_ready.wav").toString()) );
481                         iMediaObject->play();
482                 }
483
484                 QString text = "<br><b><u>Faster Application Manager</u></b><br>"
485                                            "<b>"+title+"</b><br>" + msgs.join("<br>") + "<br>";
486
487                 QRect r = QApplication::desktop()->rect();
488                 if(r.width() < r.height()) {
489                         ConfirmDialog d(false, this);
490                         d.setText(title,msgs.join("<br>"));
491                         d.exec();
492                 } else {
493 #ifdef Q_WS_MAEMO_5
494                         QMaemo5InformationBox::information(0, text, QMaemo5InformationBox::NoTimeout);
495 #else
496                         ConfirmDialog d(false, this);
497                         d.setText(title,msgs.join("<br>"));
498                         d.exec();
499 #endif
500                 }
501
502         }
503
504 }
505
506 void MainWindow::busyDialog(bool show_, QString title, QString text)
507 {
508         if( show_ ) {
509                 iDimmer->setProgress(-1);
510                 ui->menuMenu->setEnabled(false);
511                 ui->centralWidget->setEnabled(false);
512                 iWinPackageView->disableMenu();
513                 iDimmer->resizeEvent(0);
514                 iDimmer->dim(title, text);
515                 iWinPackageView->mydimmer()->resizeEvent(0);
516                 iWinPackageView->mydimmer()->dim(title, text);
517         } else {
518                 iDimmer->undim();
519                 iWinPackageView->mydimmer()->undim();
520                 ui->menuMenu->setEnabled(true);
521                 ui->centralWidget->setEnabled(true);
522                 iWinPackageView->enableMenu();
523         }
524 }
525
526 void MainWindow::on_actionAbout_triggered()
527 {
528         ConfirmDialog d(false, this);
529         d.setText("About","Faster Application Manager<br>"
530                           "<font size=\"-1\">Version " + PROGRAM_VERSION + "</font><br><br>"
531                           "(C) Heikki Holstila 2010<br>Donate using "
532                           "<a href=\"https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=6ZKRY5QFHL42A&lc=FI&item_name=Faster%20Application%20Manager"
533                           "%20for%20Maemo5&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted\">PayPal</a>");
534         d.exec();
535 }
536
537 void MainWindow::on_actionClean_triggered()
538 {
539         //if( iOperation != OpNone ) return;
540         //iOperation = OpClean;
541         iAptInterface->addQueuedOperation(AAptInterface::ModeAptGetClean);
542         iAptInterface->run(iDimmer);
543 }
544
545 void MainWindow::closeEvent(QCloseEvent *event)
546 {
547         if( iDimmer->busy() ) {
548                 iAptInterface->cancel();
549                 event->ignore();
550         } else {
551                 event->accept();
552         }
553 }
554
555 void MainWindow::on_actionView_log_triggered()
556 {
557         QByteArray log = iAptInterface->readLogFile();
558         LogView l(log, this);
559         l.exec();
560 }
561
562 void MainWindow::on_actionOptions_triggered()
563 {
564         iSettings->openWin();
565 }
566
567 void MainWindow::notifyDialog(QString title, QString msg)
568 {
569         ConfirmDialog d(false, this);
570         d.setText(title, msg);
571         d.exec();
572 }
573
574 bool MainWindow::confirmDialog(QString title, QString msg)
575 {
576         ConfirmDialog d(true, this);
577         d.setText(title, msg);
578         return d.exec();
579 }
580
581 void MainWindow::GdkEventIconThemeReload()
582 {
583         // DOES NOT EVEN WORK (at least not reliably) - disabled from the project file
584
585 #ifdef MYDEF_GTK_EXISTS
586         qDebug() << "Sending GDK icon theme reload event";
587
588         gdk_init((int*)QApplication::argc(),(gchar***)QApplication::argv());
589
590         // taken from hildon application manager
591         GdkEventClient ev;
592         ev.type = GDK_CLIENT_EVENT;
593         ev.window = NULL;
594         ev.send_event = TRUE;
595         ev.message_type = gdk_atom_intern_static_string("_GTK_LOAD_ICONTHEMES");
596         ev.data_format = 32;
597         gdk_event_send_clientmessage_toall((GdkEvent*)&ev);
598
599         while(gdk_events_pending()) {
600                 g_main_context_iteration(NULL, true);
601         }
602
603 #endif
604 }
605
606 void MainWindow::on_actionLoad_file_triggered()
607 {
608         QStringList files = QFileDialog::getOpenFileNames(this, "Open files", "/", "Files (*.deb *.install)");
609         if( files.count() > 0 ) {
610                 QStringList debs = files.filter(QRegExp(".*\\.deb$"));
611                 QStringList installs = files.filter(QRegExp(".*\\.install$"));
612                 if( debs.count()>0 && installs.count()>0 ) {
613                         ConfirmDialog d(false, this);
614                         d.setText("Error", "You can't mix different file types in your selection");
615                         d.exec();
616                         return;
617                 }
618                 if( debs.count()>0 )
619                         iDpkgInterface->loadDebFiles(debs);
620                 else if( installs.count()>0 )
621                         iAptInterface->loadInstallFiles(installs);
622         }
623 }