Architecture changed to allow easier addition of new phone books. Norwegian phonebook...
[jenirok] / src / gui / mainwindow.cpp
index 1e4de14..8d75fea 100644 (file)
 #include <QtGui/QWidget>
 #include <QtGui/QHBoxLayout>
 #include <QtGui/QMessageBox>
+#include <QtGui/QDialogButtonBox>
+#include <QtGui/QTextEdit>
+#include <QtCore/QDebug>
 #include <QMaemo5InformationBox>
-#include <QDebug>
 #include "mainwindow.h"
 #include "settingsdialog.h"
 #include "searchdialog.h"
 #include "daemon.h"
 #include "settings.h"
 #include "db.h"
+#include "source.h"
+#include "sourcecoreconfig.h"
 
 namespace
 {
@@ -39,7 +43,7 @@ namespace
 
 MainWindow::MainWindow(QWidget* parent): QMainWindow(parent),
 searchResults_(0), settingsDialog_(0), running_(false),
-toggleButton_(0), searchDialog_(0), aboutDialog_(0)
+toggleButton_(0), searchDialog_(0), aboutDialog_(0), warning_(0)
 {
     setWindowTitle(tr("Jenirok"));
     setAttribute(Qt::WA_Maemo5StackedWindow);
@@ -86,6 +90,11 @@ MainWindow::~MainWindow()
 
 void MainWindow::showSettings()
 {
+    if(warning_ && warning_->isVisible())
+    {
+        warning_->hide();
+    }
+
     if(!settingsDialog_)
     {
         settingsDialog_ = new SettingsDialog(this);
@@ -110,6 +119,33 @@ void MainWindow::toggleDaemon()
     }
     else
     {
+        if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
+        {
+            if(!warning_)
+            {
+                warning_ = new QDialog(this);
+                warning_->setWindowTitle(tr("Unable to start daemon"));
+                QHBoxLayout* warningLayout = new QHBoxLayout;
+                QTextEdit* text = new QTextEdit(tr("Daemon cannot be started because it's not allowed to connect to the Internet. You have to either allow automatic Internet connection in Jenirok settings or in global Maemo settings."));
+                text->setReadOnly(true);
+                QDialogButtonBox* buttons = new QDialogButtonBox;
+                buttons->setOrientation(Qt::Vertical);
+                QPushButton* settingsButton = new QPushButton(tr("Open settings"));
+                connect(settingsButton, SIGNAL(pressed()), this, SLOT(showSettings()));
+                QPushButton* okButton = new QPushButton(tr("Close"));
+                connect(okButton, SIGNAL(pressed()), warning_, SLOT(hide()));
+                buttons->addButton(settingsButton, QDialogButtonBox::YesRole);
+                buttons->addButton(okButton, QDialogButtonBox::AcceptRole);
+                warningLayout->addWidget(text);
+                warningLayout->addWidget(buttons);
+                warning_->setLayout(warningLayout);
+            }
+
+            warning_->show();
+
+            return;
+        }
+
         readyText = tr("Daemon was successfully started.");
         failText = tr("Unable to start daemon.");
         buttonText = tr("Stop daemon");
@@ -132,16 +168,17 @@ void MainWindow::toggleDaemon()
 
 void MainWindow::openSearch()
 {
-    DB::connect();
+    Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source"));
+    SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
 
-    QString username = Settings::instance()->get("eniro_username");
-    QString password = Settings::instance()->get("eniro_password");
+    Q_ASSERT(config != 0);
 
-    DB::disconnect();
+    bool readyToSearch = config->readyToSearch();
+    delete config;
 
-    if(username.isEmpty() || password.isEmpty())
+    if(!readyToSearch)
     {
-        QMessageBox::information(this, tr("Info"), tr("You need to set Eniro login details in settings before using this feature."));
+        QMessageBox::information(this, tr("Info"), tr("You need to set login details or other options in settings before using this feature."));
         return;
     }