view settings.cpp @ 11:98485ebbdb86 default tip

LayoutView: WidgetViewBox fix
author Goffi <goffi@goffi.org>
date Sat, 27 Aug 2011 00:31:20 +0200
parents c63d67895cbe
children
line wrap: on
line source

/*
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"
#include "bellaciao.h"
#include "layout_designer.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 add the settings in tabs

    // Contact list Layout
    QVBoxLayout *layout =  new QVBoxLayout;
    LayoutDesigner* contact_list_layout = new LayoutDesigner;
    layout->addWidget(contact_list_layout);
    contactListLayoutTab->setLayout(layout);
    

    //We fill the profiles list
    m_bridge = Bridge::getBridge();
    m_session = Session::getSession();

    const QList<QString>& profiles = m_bridge->getProfilesList();
    const QString& default_profile = m_bridge->getProfileName("@DEFAULT@");
    const QList<QString>& checked_profiles = m_session->getProfiles(); 
    
    foreach(const QString& profile, profiles) {
        ProfileListWidgetItem *item = new ProfileListWidgetItem(profile, profileList, profile==default_profile);
        item->setFlags(item->flags() | ItemIsUserCheckable);
        item->setCheckState(checked_profiles.contains(profile)?Checked:Unchecked);
    }
    
    connect(profileList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(profileChanged(QListWidgetItem*)));
    connect(profileList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(profileCurrentChanged(QListWidgetItem*, QListWidgetItem*)));

    //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)
{
}

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() //Save settings to session
{
    // We save profiles
    QList<QString> previous_profiles;
    QList<QString>& profiles = m_session->getProfiles();
    previous_profiles = profiles;
    profiles.clear();
    for (int i=0; i<profileList->count(); i++)
    {
        ProfileListWidgetItem* item = dynamic_cast<ProfileListWidgetItem*>(profileList->item(i));
        if (item->checkState() == Checked){
            profiles.append(item->getProfile());
        }
    }

    if (previous_profiles!=profiles) //The profiles checked are differents, we refill the contactList
        Bellaciao::getInstance()->connectProfiles();

    close();
        
}

void Settings::cancelSettings()
{
    close();
}