diff settings.cpp @ 2:fee291c8d42a

settings: profiles management first draft
author Goffi <goffi@goffi.org>
date Mon, 08 Aug 2011 10:30:23 +0200
parents parameters.cpp@22b44846b04b
children 220e5619bf87
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/settings.cpp	Mon Aug 08 10:30:23 2011 +0200
@@ -0,0 +1,175 @@
+/*
+Bellaciao: a Salut à Toi frontend
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "settings.h"
+
+using namespace Qt;
+
+Settings* Settings::_instance = 0;
+
+ProfileListWidgetItem::ProfileListWidgetItem(const QString & profile, QListWidget * parent, bool isDefault)
+    :QListWidgetItem(profile, parent)
+{
+    m_profile = profile;
+    if (isDefault) {
+        setText(text() + QObject::tr(" (default)"));
+        QFont _font = font();
+        _font.setWeight(QFont::Bold);
+        setFont(_font);
+    }
+
+}
+
+const QString& ProfileListWidgetItem::getProfile()
+{
+    return m_profile;
+}
+
+Settings::Settings(QWidget *parent)
+    : QWidget(parent)
+{
+    setupUi(this);
+
+    this->setAttribute(WA_DeleteOnClose);
+
+    //We fill the profiles list
+    m_bridge = Bridge::getBridge();
+    const QList<QString>& profiles = m_bridge->getProfilesList();
+    const QString& default_profile = m_bridge->getProfileName("@DEFAULT@");
+    foreach(const QString& profile, profiles) {
+        ProfileListWidgetItem *item = new ProfileListWidgetItem(profile, profileList, profile==default_profile);
+        item->setFlags(item->flags() | ItemIsUserCheckable);
+        item->setCheckState(Checked);
+    }
+    
+    connect(profileList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(profileChanged(QListWidgetItem*)));
+    connect(profileList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(profileCurrentChanged(QListWidgetItem*, QListWidgetItem*)));
+
+    //Profiles manipulation buttons
+    connect(buttonsGroupConfirm, SIGNAL(accepted()), this, SLOT(saveSettings()));
+
+    //Confirmation buttons
+    connect(buttonsGroupConfirm, SIGNAL(accepted()), this, SLOT(saveSettings()));
+    connect(buttonsGroupConfirm, SIGNAL(rejected()), this, SLOT(cancelSettings()));
+}
+
+Settings::~Settings()
+{
+    _instance = 0;
+}
+
+Settings* Settings::getDialog() {
+    if (_instance == 0) {
+        _instance = new Settings;
+    }
+    
+    return _instance;
+}
+
+//Profile settings slots
+void Settings::profileChanged(QListWidgetItem* item)
+{
+    qDebug() << "profileChanged";
+}
+
+void Settings::profileCurrentChanged(QListWidgetItem* current, QListWidgetItem* previous)
+{
+    if (!current) {
+        profileData->setEnabled(false);
+        customServerGroup->setEnabled(false);
+        customServerGroup->setChecked(false);
+        buttonDelete->setEnabled(false);
+        buttonSetDefault->setEnabled(false);
+        jidField->clear();
+        passwordField->clear();
+        serverField->clear();
+        portField->clear();
+        return;
+    }
+
+    ProfileListWidgetItem *item = dynamic_cast<ProfileListWidgetItem*>(current);
+
+    profileData->setEnabled(true);
+    customServerGroup->setEnabled(true);
+    jidField->setEnabled(true);
+    passwordField->setEnabled(true);
+    buttonDelete->setEnabled(true);
+    buttonSetDefault->setEnabled(true);
+    const QString& jid = m_bridge->getParamA("JabberID", "Connection", "value", item->getProfile());
+    const QString& password = m_bridge->getParamA("Password", "Connection", "value", item->getProfile());
+    jidField->setText(jid);
+    passwordField->setText(password);
+}
+
+
+void Settings::on_buttonNew_clicked() {
+    bool ok;
+    QString name = QInputDialog::getText(this, tr("New profile"), tr("Please enter profile name: "), QLineEdit::Normal, QString(), &ok);
+    if (ok && !name.isEmpty()) {
+        int success = m_bridge->createProfile(name);
+        ProfileListWidgetItem *item;
+        
+        switch (success) {
+            case 0: //success
+                item = new ProfileListWidgetItem(name, profileList);
+                item->setFlags(item->flags() | ItemIsUserCheckable);
+                item->setCheckState(Checked);
+                break;
+            case 1: //profile name conflict
+                QMessageBox::warning(this, tr("Profile name conflict"), tr("A profile with this name already exists. Please choose an other name"));
+                break;
+            default:
+                QMessageBox::warning(this, tr("Profile creation error"), tr("Can't create profile"));
+                
+        }
+    }
+}
+
+
+void Settings::on_buttonDelete_clicked() {
+    ProfileListWidgetItem *item = dynamic_cast<ProfileListWidgetItem*>(profileList->currentItem());
+
+    if ( QMessageBox::question (
+            this,
+            tr("Delete profile?"),
+            tr("Do you really want to delete profile \"%1\" ?").arg(item->getProfile()),
+            QMessageBox::Yes | QMessageBox::No,
+            QMessageBox::No
+            ) == QMessageBox::Yes
+       )
+    {
+        int success = m_bridge->deleteProfile(item->getProfile());
+        if (success==0)
+            delete(item);
+        else
+            QMessageBox::warning(this, tr("Profile deletion error"), tr("Can't delete profile"));
+    }
+
+    
+}
+
+
+//Global settings slots
+void Settings::saveSettings()
+{
+}
+
+void Settings::cancelSettings()
+{
+    this->close();
+}