root/trunk/qt4-gui/src/dialogs/editcategorydlg.cpp

Revision 6466, 4.1 kB (checked in by flynd, 4 months ago)

Use const pointer for owner objects that are fetched only for read access.

  • Property svn:eol-style set to native
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of Licq, an instant messaging client for UNIX.
4 * Copyright (C) 2004-2006 Licq developers
5 *
6 * Licq is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Licq is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Licq; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 */
20
21#include "editcategorydlg.h"
22
23#include <QComboBox>
24#include <QDialogButtonBox>
25#include <QGridLayout>
26#include <QLineEdit>
27#include <QTextCodec>
28
29#include <licq_interestcodes.h>
30#include <licq_organizationcodes.h>
31#include <licq_backgroundcodes.h>
32
33#include "helpers/usercodec.h"
34#include "helpers/support.h"
35
36using namespace LicqQtGui;
37/* TRANSLATOR LicqQtGui::EditCategoryDlg */
38
39EditCategoryDlg::EditCategoryDlg(ICQUserCategory* cat, QWidget* parent)
40  : QDialog(parent),
41    myUserCat(cat->GetCategory())
42{
43  Support::setWidgetProps(this, "EditCategoryDlg");
44  setAttribute(Qt::WA_DeleteOnClose, true);
45
46  QString title = "Licq - Edit @ Category";
47
48  unsigned short tableSize, i = 0;
49
50  switch (myUserCat)
51  {
52    case CAT_INTERESTS:
53      myNumCats = MAX_CAT;
54      getEntry = GetInterestByIndex;
55      tableSize = NUM_INTERESTS;
56      title.replace("@", tr("Personal Interests"));
57      break;
58    case CAT_ORGANIZATION:
59      myNumCats = MAX_CAT - 1;
60      getEntry = GetOrganizationByIndex;
61      tableSize = NUM_ORGANIZATIONS;
62      title.replace("@", tr("Organization, Affiliation, Group"));
63      break;
64    case CAT_BACKGROUND:
65      myNumCats = MAX_CAT - 1;
66      getEntry = GetBackgroundByIndex;
67      tableSize = NUM_BACKGROUNDS;
68      title.replace("@", tr("Past Background"));
69      break;
70    default:
71      close();
72      return;
73  }
74
75  setWindowTitle(title);
76
77  QGridLayout* top_lay = new QGridLayout(this);
78
79  for (; i < myNumCats ; i++)
80  {
81    myCats[i] = new QComboBox();
82    myCats[i]->addItem(tr("Unspecified"));
83
84    int selected = 0;
85    const char* descr;
86    unsigned short selection_id;
87    if (!cat->Get(i, &selection_id, &descr))
88    {
89      selection_id = 0;
90      descr = "";
91    }
92
93    for (unsigned short j = 0; j < tableSize ; j++)
94    {
95      myCats[i]->addItem(getEntry(j)->szName);
96      if (getEntry(j)->nCode == selection_id)
97        selected = j + 1;
98    }
99
100    myCats[i]->setCurrentIndex(selected);
101    connect(myCats[i], SIGNAL(activated(int)), SLOT(checkEnabled()));
102    top_lay->addWidget(myCats[i], i, 0);
103
104    myDescr[i] = new QLineEdit();
105    myDescr[i]->setMinimumWidth(300);
106    myDescr[i]->setMaxLength(MAX_CATEGORY_SIZE);
107    myDescr[i]->setText(descr);
108    myDescr[i]->setEnabled(selection_id != 0);
109    top_lay->addWidget(myDescr[i], i, 1);
110  }
111
112  QDialogButtonBox* buttons = new QDialogButtonBox(
113      QDialogButtonBox::Ok |
114      QDialogButtonBox::Cancel);
115  connect(buttons, SIGNAL(accepted()), SLOT(ok()));
116  connect(buttons, SIGNAL(rejected()), SLOT(close()));
117
118  top_lay->setRowStretch(i++, 1);
119  top_lay->addWidget(buttons, i++, 0, 1, 2);
120  top_lay->setColumnStretch(1, 1);
121
122  show();
123}
124
125void EditCategoryDlg::ok()
126{
127  const ICQOwner* o = gUserManager.FetchOwner(LICQ_PPID, LOCK_R);
128  if (o != NULL)
129  {
130    QTextCodec* codec = UserCodec::codecForICQUser(o);
131    gUserManager.DropOwner(o);
132
133    ICQUserCategory* cat = new ICQUserCategory(myUserCat);
134    for (unsigned short i = 0; i < myNumCats; i++)
135    {
136      if (myCats[i]->currentIndex() != 0)
137        cat->AddCategory(getEntry(myCats[i]->currentIndex() - 1)->nCode,
138            codec->fromUnicode(myDescr[i]->text()));
139    }
140
141    emit updated(cat);
142  }
143  close();
144}
145
146void EditCategoryDlg::checkEnabled()
147{
148  for (unsigned short i = 0; i < myNumCats; i++)
149    myDescr[i]->setEnabled(myCats[i]->currentIndex() != 0);
150}
Note: See TracBrowser for help on using the browser.