~ubuntu-branches/ubuntu/utopic/psi/utopic

« back to all changes in this revision

Viewing changes to src/proxy.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:
36
36
//Added by qt3to4:
37
37
#include <QHBoxLayout>
38
38
#include <QList>
 
39
#include <QDebug>
39
40
#include "common.h"
40
41
#include "iconwidget.h"
 
42
#include "psioptions.h"
41
43
 
42
44
static QDomElement textTag(QDomDocument &doc, const QString &name, const QString &content)
43
45
{
143
145
        useAuth = false;
144
146
}
145
147
 
 
148
#define PASSWORDKEY "Cae1voo:ea}fae2OCai|f1il"
 
149
 
 
150
void ProxySettings::toOptions(OptionsTree* o, QString base) const
 
151
{
 
152
        o->setOption(base + ".host", host);
 
153
        o->setOption(base + ".port", port);
 
154
        o->setOption(base + ".url", url);
 
155
        o->setOption(base + ".useAuth", useAuth);
 
156
        o->setOption(base + ".user", user);
 
157
        o->setOption(base + ".pass", encodePassword(pass, PASSWORDKEY));
 
158
}
 
159
 
 
160
void ProxySettings::fromOptions(OptionsTree* o, QString base)
 
161
{
 
162
        host = o->getOption(base + ".host").toString();
 
163
        port = o->getOption(base + ".port").toInt();
 
164
        url = o->getOption(base + ".url").toString();
 
165
        useAuth = o->getOption(base + ".useAuth").toBool();
 
166
        user = o->getOption(base + ".user").toString();
 
167
        pass = decodePassword(o->getOption(base + ".pass").toString(), PASSWORDKEY);
 
168
}
 
169
        
 
170
        
146
171
QDomElement ProxySettings::toXml(QDomDocument *doc) const
147
172
{
148
173
        QDomElement e = doc->createElement("proxySettings");
303
328
        int last;
304
329
};
305
330
 
306
 
ProxyDlg::ProxyDlg(const ProxyItemList &list, const QStringList &methods, int def, QWidget *parent)
 
331
ProxyDlg::ProxyDlg(const ProxyItemList &list, const QStringList &methods, const QString &def, QWidget *parent)
307
332
        : QDialog(parent)
308
333
{
309
334
        setAttribute(Qt::WA_DeleteOnClose);
331
356
        cb_type->insertStringList(methods);
332
357
        connect(cb_type, SIGNAL(activated(int)), SLOT(cb_activated(int)));
333
358
 
334
 
        for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it)
 
359
        int defIdx = -1;
 
360
        int i = 0;
 
361
        for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it) {
335
362
                lbx_proxy->insertItem((*it).name);
 
363
                if ((*it).id == def) defIdx= i;
 
364
                ++i;
 
365
        }
336
366
        if(!list.isEmpty()) {
337
 
                if(def < 0)
338
 
                        def = 0;
339
 
                lbx_proxy->setCurrentItem(def);
 
367
                if(defIdx < 0)
 
368
                        defIdx = 0;
 
369
                lbx_proxy->setCurrentItem(defIdx);
340
370
                selectCurrent();
341
371
        }
342
372
 
354
384
void ProxyDlg::proxy_new()
355
385
{
356
386
        ProxyItem s;
357
 
        s.id = -1; // id of -1 means 'new'
 
387
        s.id = ""; // id of "" means 'new'
358
388
        s.name = getUniqueName();
359
389
        d->list += s;
360
390
 
530
560
        delete d;
531
561
}
532
562
 
533
 
int ProxyChooser::currentItem() const
534
 
{
535
 
        return d->cb_proxy->currentItem();
536
 
}
537
 
 
538
 
void ProxyChooser::setCurrentItem(int x)
539
 
{
540
 
        d->cb_proxy->setCurrentItem(x);
541
 
}
 
563
QString ProxyChooser::currentItem() const
 
564
{
 
565
        return d->cb_proxy->itemData(d->cb_proxy->currentItem()).toString();
 
566
}
 
567
 
 
568
void ProxyChooser::setCurrentItem(const QString &id)
 
569
{
 
570
        d->cb_proxy->setCurrentItem(d->cb_proxy->findData(id));
 
571
}
 
572
 
 
573
void ProxyChooser::pm_settingsChangedApply()
 
574
{
 
575
        disconnect(d->m, SIGNAL(settingsChanged()), this, SLOT(pm_settingsChangedApply()));
 
576
        QString prev = currentItem();
 
577
        buildComboBox();
 
578
        prev = d->m->lastEdited();
 
579
        int x = d->cb_proxy->findData(prev);
 
580
        if(x == -1) {
 
581
                d->cb_proxy->setCurrentItem(0);
 
582
        } else {
 
583
                d->cb_proxy->setCurrentItem(x);
 
584
        }
 
585
}
 
586
 
542
587
 
543
588
void ProxyChooser::pm_settingsChanged()
544
589
{
545
 
        int x = d->cb_proxy->currentItem();
 
590
        QString prev = currentItem();
546
591
        buildComboBox();
547
 
        if(x >= 1) {
548
 
                x = d->m->findOldIndex(x-1);
549
 
                if(x == -1)
550
 
                        d->cb_proxy->setCurrentItem(0);
551
 
                else
552
 
                        d->cb_proxy->setCurrentItem(x+1);
553
 
        }
554
 
        else {
555
 
                x = d->m->lastEdited();
556
 
                if(x != -1)
557
 
                        d->cb_proxy->setCurrentItem(x+1);
 
592
        int x = d->cb_proxy->findData(prev);
 
593
        if(x == -1) {
 
594
                d->cb_proxy->setCurrentItem(0);
 
595
        } else {
 
596
                d->cb_proxy->setCurrentItem(x);
558
597
        }
559
598
}
560
599
 
561
600
void ProxyChooser::buildComboBox()
562
601
{
563
602
        d->cb_proxy->clear();
564
 
        d->cb_proxy->insertItem(tr("None"));
 
603
        d->cb_proxy->addItem(tr("None"), "");
565
604
        ProxyItemList list = d->m->itemList();
566
 
        for(ProxyItemList::ConstIterator it = list.begin(); it != list.end(); ++it)
567
 
                d->cb_proxy->insertItem((*it).name);
 
605
        foreach(ProxyItem pi, list) {
 
606
                d->cb_proxy->addItem(pi.name, pi.id);
 
607
        }
568
608
}
569
609
 
570
610
void ProxyChooser::doOpen()
571
611
{
572
 
        int x = d->cb_proxy->currentItem();
573
 
        if(x < 1)
574
 
                x = -1;
575
 
        else
576
 
                --x;
 
612
        QString x = d->cb_proxy->itemData(d->cb_proxy->currentItem()).toString();
 
613
        connect(d->m, SIGNAL(settingsChanged()), SLOT(pm_settingsChangedApply()));
577
614
        d->m->openDialog(x);
578
615
}
579
616
 
593
630
public:
594
631
        Private() {}
595
632
 
596
 
        ProxyItemList list;
597
633
        QPointer<ProxyDlg> pd;
598
634
        QList<int> prevMap;
599
 
        int lastEdited;
 
635
        QString lastEdited;
 
636
        OptionsTree *o;
 
637
        
 
638
        void itemToOptions(ProxyItem pi) {
 
639
                QString base = "proxies." + pi.id;
 
640
                pi.settings.toOptions( o, base);
 
641
                o->setOption(base + ".name", pi.name);
 
642
                o->setOption(base + ".type", pi.type);
 
643
        }
600
644
};
601
645
 
602
 
ProxyManager::ProxyManager(QObject *parent)
603
 
:QObject(parent)
 
646
ProxyManager::ProxyManager(OptionsTree *opt, QObject *parent)
 
647
                : QObject(parent)
604
648
{
605
649
        d = new Private;
 
650
        d->o = opt;
606
651
}
607
652
 
608
653
ProxyManager::~ProxyManager()
617
662
 
618
663
ProxyItemList ProxyManager::itemList() const
619
664
{
620
 
        return d->list;
 
665
        QList<ProxyItem> proxies;
 
666
        QString opt = "proxies";
 
667
        QStringList keys = d->o->getChildOptionNames(opt, true, true);
 
668
        foreach(QString key, keys) {
 
669
                proxies += getItem(key.mid(opt.length()+1));
 
670
        }
 
671
        return proxies;
621
672
}
622
673
 
623
 
const ProxyItem & ProxyManager::getItem(int x) const
 
674
ProxyItem ProxyManager::getItem(const QString &x) const
624
675
{
625
 
        return d->list[x];
 
676
        QString base = "proxies." + x;
 
677
        ProxyItem pi;
 
678
        pi.settings.fromOptions( d->o, base);
 
679
        pi.name = d->o->getOption(base + ".name").toString();
 
680
        pi.type = d->o->getOption(base + ".type").toString();
 
681
        pi.id = x;
 
682
        return pi;
626
683
}
627
684
 
628
 
int ProxyManager::lastEdited() const
 
685
QString ProxyManager::lastEdited() const
629
686
{
630
687
        return d->lastEdited;
631
688
}
632
689
 
633
 
void ProxyManager::setItemList(const ProxyItemList &list)
 
690
void ProxyManager::migrateItemList(const ProxyItemList &list)
634
691
{
635
 
        d->list = list;
636
 
        assignIds();
 
692
        foreach(ProxyItem pi, list) {
 
693
                d->itemToOptions(pi);
 
694
        }
637
695
}
638
696
 
639
697
QStringList ProxyManager::methodList() const
645
703
        return list;
646
704
}
647
705
 
648
 
void ProxyManager::openDialog(int def)
 
706
void ProxyManager::openDialog(QString def)
649
707
{
650
708
        if(d->pd)
651
709
                bringToFront(d->pd);
652
710
        else {
653
 
                d->pd = new ProxyDlg(d->list, methodList(), def, 0);
 
711
                d->pd = new ProxyDlg(itemList(), methodList(), def, 0);
654
712
                connect(d->pd, SIGNAL(applyList(const ProxyItemList &, int)), SLOT(pd_applyList(const ProxyItemList &, int)));
655
713
                d->pd->show();
656
714
        }
658
716
 
659
717
void ProxyManager::pd_applyList(const ProxyItemList &list, int x)
660
718
{
661
 
        d->list = list;
662
 
        d->lastEdited = x;
663
 
 
664
 
        // grab old id list
665
 
        d->prevMap.clear();
666
 
        for(ProxyItemList::ConstIterator it = d->list.begin(); it != d->list.end(); ++it)
667
 
                d->prevMap += (*it).id;
668
 
        assignIds(); // re-assign proper ids
669
 
 
 
719
        QSet<QString> current;
 
720
        
 
721
        QString opt = "proxies";
 
722
        QStringList old = d->o->getChildOptionNames(opt, true, true);
 
723
        for (int i=0; i < old.size(); i++) {
 
724
                old[i] = old[i].mid(opt.length()+1);
 
725
        }
 
726
        
 
727
        // Update all
 
728
        int idx = 0;
 
729
        int i = 0;
 
730
        foreach(ProxyItem pi, list) {
 
731
                if (pi.id.isEmpty()) {
 
732
                        do {
 
733
                                pi.id = "a"+QString::number(idx++);
 
734
                        } while (old.contains(pi.id) || current.contains(pi.id));
 
735
                }
 
736
                d->itemToOptions(pi);
 
737
                current += pi.id;
 
738
                
 
739
                if (i++ == x) d->lastEdited = pi.id;
 
740
        }
 
741
        
 
742
        // and remove removed
 
743
        foreach(QString key, old.toSet() - current) {
 
744
                d->o->removeOption("proxies." + key, true);
 
745
                emit proxyRemoved(key);
 
746
        }       
 
747
        
670
748
        settingsChanged();
671
749
}
672
 
 
673
 
void ProxyManager::assignIds()
674
 
{
675
 
        int n = 0;
676
 
        for(ProxyItemList::Iterator it = d->list.begin(); it != d->list.end(); ++it)
677
 
                (*it).id = n++;
678
 
}
679
 
 
680
 
int ProxyManager::findOldIndex(int x) const
681
 
{
682
 
        int newPos = 0;
683
 
        bool found = false;
684
 
        for(QList<int>::ConstIterator it = d->prevMap.begin(); it != d->prevMap.end(); ++it) {
685
 
                if(*it == x) {
686
 
                        found = true;
687
 
                        break;
688
 
                }
689
 
                ++newPos;
690
 
        }
691
 
        if(found)
692
 
                return newPos;
693
 
        else
694
 
                return -1;
695
 
}