~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/accountscombobox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * accountscombobox.cpp
3
 
 * Copyright (C) 2001, 2002  Justin Karneges
 
3
 * Copyright (C) 2001-2008  Justin Karneges, Michail Pishchagin
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or
6
6
 * modify it under the terms of the GNU General Public License
23
23
#include "psiaccount.h"
24
24
#include "psicontactlist.h"
25
25
 
26
 
AccountsComboBox::AccountsComboBox(PsiCon *_psi, QWidget *parent, bool online_only)
27
 
:QComboBox(parent), onlineOnly(online_only)
 
26
AccountsComboBox::AccountsComboBox(QWidget* parent)
 
27
        : QComboBox(parent)
 
28
        , controller_(0)
 
29
        , account_(0)
 
30
        , onlineOnly_(false)
28
31
{
29
 
        psi = _psi;
30
 
        // TODO: Status changes of accounts should be notified when onlineOnly is true
31
 
        connect(psi, SIGNAL(accountCountChanged()), this, SLOT(updateAccounts()));
32
 
        connect(psi, SIGNAL(destroyed()), SLOT(deleteMe()));
 
32
        setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
33
33
        connect(this, SIGNAL(activated(int)), this, SLOT(changeAccount()));
34
 
        if (online_only)
35
 
                connect(psi, SIGNAL(accountActivityChanged()), this, SLOT(updateAccounts()));
36
 
 
37
 
        pa = 0;
38
 
        setSizePolicy(QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ));
39
 
 
40
 
        if(psi->contactList()->haveEnabledAccounts())
41
 
                setAccount(psi->contactList()->enabledAccounts().first());
42
34
}
43
35
 
44
36
AccountsComboBox::~AccountsComboBox()
45
37
{
46
38
}
47
39
 
48
 
void AccountsComboBox::setAccount(PsiAccount *_pa)
49
 
{
50
 
        pa = _pa;
 
40
PsiAccount* AccountsComboBox::account() const
 
41
{
 
42
        return account_;
 
43
}
 
44
 
 
45
void AccountsComboBox::setAccount(PsiAccount* account)
 
46
{
 
47
        account_ = account;
 
48
        updateAccounts();
 
49
}
 
50
 
 
51
PsiCon* AccountsComboBox::controller() const
 
52
{
 
53
        return controller_;
 
54
}
 
55
 
 
56
void AccountsComboBox::setController(PsiCon* controller)
 
57
{
 
58
        if (controller_) {
 
59
                disconnect(controller_, SIGNAL(accountCountChanged()), this, SLOT(updateAccounts()));
 
60
                disconnect(controller_, SIGNAL(accountActivityChanged()), this, SLOT(updateAccounts()));
 
61
        }
 
62
 
 
63
        controller_ = controller;
 
64
 
 
65
        if (controller_) {
 
66
                connect(controller_, SIGNAL(accountCountChanged()), this, SLOT(updateAccounts()));
 
67
                connect(controller_, SIGNAL(accountActivityChanged()), this, SLOT(updateAccounts()));
 
68
        }
 
69
 
 
70
        if (controller_->contactList()->haveEnabledAccounts()) {
 
71
                setAccount(controller_->contactList()->enabledAccounts().first());
 
72
        }
 
73
 
 
74
        updateAccounts();
 
75
}
 
76
 
 
77
bool AccountsComboBox::onlineOnly() const
 
78
{
 
79
        return onlineOnly_;
 
80
}
 
81
 
 
82
void AccountsComboBox::setOnlineOnly(bool onlineOnly)
 
83
{
 
84
        onlineOnly_ = onlineOnly;
51
85
        updateAccounts();
52
86
}
53
87
 
54
88
void AccountsComboBox::changeAccount()
55
89
{
56
 
        int i = currentItem();
57
 
 
58
 
        int n = 0;
59
 
        bool found = false;
60
 
        foreach(PsiAccount* p, psi->contactList()->enabledAccounts()) {
61
 
                if (!onlineOnly || p->loggedIn()) {
62
 
                        if(i == n) {
63
 
                                pa = p;
64
 
                                found = true;
65
 
                                break;
66
 
                        }
67
 
                        ++n;
68
 
                }
69
 
        }
70
 
        if(!found)
71
 
                pa = 0;
72
 
 
73
 
        activated(pa);
 
90
        account_ = 0;
 
91
        if (currentIndex() >= 0 && currentIndex() < accounts().count())
 
92
                account_ = accounts().at(currentIndex());
 
93
        emit activated(account_);
74
94
}
75
95
 
76
96
void AccountsComboBox::updateAccounts()
77
97
{
78
98
        clear();
79
 
        int n = 0;
80
 
        bool found = false;
81
 
        PsiAccount *firstAccount = 0;
82
 
        foreach(PsiAccount* p, psi->contactList()->enabledAccounts()) {
83
 
                if (!onlineOnly || p->loggedIn()) {
84
 
                        insertItem(p->nameWithJid());
85
 
                        if(p == pa) {
86
 
                                setCurrentItem(n);
87
 
                                found = true;
88
 
                        }
89
 
                        if (!firstAccount)
90
 
                                firstAccount = p;
91
 
                        ++n;
92
 
                }
93
 
        }
94
 
        if(!found) {
95
 
                // choose a different account
96
 
                pa = firstAccount;
97
 
                activated(pa);
98
 
        }
 
99
 
 
100
        foreach(PsiAccount* account, accounts())
 
101
                insertItem(account->nameWithJid());
 
102
 
 
103
        if (accounts().indexOf(account_) == -1) {
 
104
                account_ = accounts().isEmpty() ? 0 : accounts().first();
 
105
                emit activated(account_);
 
106
        }
 
107
        setCurrentIndex(accounts().indexOf(account_));
99
108
}
100
109
 
101
 
void AccountsComboBox::deleteMe()
 
110
QList<PsiAccount*> AccountsComboBox::accounts() const
102
111
{
103
 
        delete this;
 
112
        QList<PsiAccount*> result;
 
113
        if (controller_) {
 
114
                foreach(PsiAccount* account, controller_->contactList()->enabledAccounts())
 
115
                        if (!onlineOnly_ || account->isAvailable())
 
116
                                result << account;
 
117
        }
 
118
 
 
119
        return result;
104
120
}
105
 
 
106