1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2004-2013 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "editcategorydlg.h"
21 
22 #include <QComboBox>
23 #include <QDialogButtonBox>
24 #include <QGridLayout>
25 #include <QLineEdit>
26 
27 #include <licq/icq/icqdata.h>
28 #include <licq/icq/user.h>
29 #include <licq/plugin/pluginmanager.h>
30 
31 #include "helpers/support.h"
32 
33 using namespace LicqQtGui;
34 /* TRANSLATOR LicqQtGui::EditCategoryDlg */
35 
EditCategoryDlg(Licq::UserCat cat,const Licq::UserCategoryMap & category,QWidget * parent)36 EditCategoryDlg::EditCategoryDlg(Licq::UserCat cat, const Licq::UserCategoryMap& category, QWidget* parent)
37   : QDialog(parent),
38     myUserCat(cat)
39 {
40   Support::setWidgetProps(this, "EditCategoryDlg");
41   setAttribute(Qt::WA_DeleteOnClose, true);
42 
43   QString title = "Licq - Edit @ Category";
44 
45   Licq::IcqData::Ptr icq = plugin_internal_cast<Licq::IcqData>(
46       Licq::gPluginManager.getProtocolPlugin(ICQ_PPID));
47   if (!icq)
48   {
49     close();
50     return;
51   }
52 
53   unsigned short tableSize;
54 
55   switch (myUserCat)
56   {
57     case Licq::CAT_INTERESTS:
58       myNumCats = Licq::MAX_CATEGORIES;
59       myIcqCategoryType = Licq::IcqCatTypeInterest;
60       tableSize = Licq::NUM_INTERESTS;
61       title.replace("@", tr("Personal Interests"));
62       break;
63     case Licq::CAT_ORGANIZATION:
64       myNumCats = Licq::MAX_CATEGORIES - 1;
65       myIcqCategoryType = Licq::IcqCatTypeOrganization;
66       tableSize = Licq::NUM_ORGANIZATIONS;
67       title.replace("@", tr("Organization, Affiliation, Group"));
68       break;
69     case Licq::CAT_BACKGROUND:
70       myNumCats = Licq::MAX_CATEGORIES - 1;
71       myIcqCategoryType = Licq::IcqCatTypeBackground;
72       tableSize = Licq::NUM_BACKGROUNDS;
73       title.replace("@", tr("Past Background"));
74       break;
75     default:
76       close();
77       return;
78   }
79 
80   setWindowTitle(title);
81 
82   QGridLayout* top_lay = new QGridLayout(this);
83 
84   int i = 0;
85   Licq::UserCategoryMap::const_iterator it = category.begin();
86   for (; i < myNumCats ; i++)
87   {
88     myCats[i] = new QComboBox();
89     myCats[i]->addItem(tr("Unspecified"));
90 
91     int selected = 0;
92     unsigned short selection_id;
93     QString descr;
94     if (it != category.end())
95     {
96       selection_id = it->first;
97       descr = QString::fromUtf8(it->second.c_str());
98       ++it;
99     }
100     else
101     {
102       selection_id = 0;
103       descr = "";
104     }
105 
106     for (int j = 0; j < tableSize ; j++)
107     {
108       const struct Licq::IcqCategory* icqcat = icq->getCategoryByIndex(myIcqCategoryType, j);
109       myCats[i]->addItem(QString::fromUtf8(icqcat->szName));
110       if (icqcat->nCode == selection_id)
111         selected = j + 1;
112     }
113 
114     myCats[i]->setCurrentIndex(selected);
115     connect(myCats[i], SIGNAL(activated(int)), SLOT(checkEnabled()));
116     top_lay->addWidget(myCats[i], i, 0);
117 
118     myDescr[i] = new QLineEdit();
119     myDescr[i]->setMinimumWidth(300);
120     myDescr[i]->setMaxLength(Licq::MAX_CATEGORY_SIZE);
121     myDescr[i]->setText(descr);
122     myDescr[i]->setEnabled(selection_id != 0);
123     top_lay->addWidget(myDescr[i], i, 1);
124   }
125 
126   QDialogButtonBox* buttons = new QDialogButtonBox(
127       QDialogButtonBox::Ok |
128       QDialogButtonBox::Cancel);
129   connect(buttons, SIGNAL(accepted()), SLOT(ok()));
130   connect(buttons, SIGNAL(rejected()), SLOT(close()));
131 
132   top_lay->setRowStretch(i++, 1);
133   top_lay->addWidget(buttons, i++, 0, 1, 2);
134   top_lay->setColumnStretch(1, 1);
135 
136   show();
137 }
138 
ok()139 void EditCategoryDlg::ok()
140 {
141   Licq::IcqData::Ptr icq = plugin_internal_cast<Licq::IcqData>(
142       Licq::gPluginManager.getProtocolPlugin(ICQ_PPID));
143   if (!icq)
144   {
145     close();
146     return;
147   }
148 
149   Licq::UserCategoryMap cat;
150   for (unsigned short i = 0; i < myNumCats; i++)
151   {
152     if (myCats[i]->currentIndex() != 0)
153     {
154       const struct Licq::IcqCategory* icqcat = icq->getCategoryByIndex(myIcqCategoryType, myCats[i]->currentIndex() - 1);
155       cat[icqcat->nCode] = myDescr[i]->text().toUtf8().constData();
156     }
157   }
158 
159   emit updated(myUserCat, cat);
160 
161   close();
162 }
163 
checkEnabled()164 void EditCategoryDlg::checkEnabled()
165 {
166   for (unsigned short i = 0; i < myNumCats; i++)
167     myDescr[i]->setEnabled(myCats[i]->currentIndex() != 0);
168 }
169