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

Revision 6352, 12.8 kB (checked in by flynd, 5 months ago)

Reverted r6351. If definitions from a class is directly used by a class, the header should be included.

  • 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) 1999-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 "searchuserdlg.h"
22
23#include "config.h"
24
25#include <QCheckBox>
26#include <QComboBox>
27#include <QDialogButtonBox>
28#include <QGridLayout>
29#include <QGroupBox>
30#include <QHBoxLayout>
31#include <QLabel>
32#include <QLineEdit>
33#include <QPushButton>
34#include <QTextCodec>
35#include <QTreeWidget>
36#include <QVBoxLayout>
37
38#include <licq_countrycodes.h>
39#include <licq_icqd.h>
40#include <licq_languagecodes.h>
41#include <licq_user.h>
42
43#include "core/gui-defines.h"
44#include "core/licqgui.h"
45#include "core/messagebox.h"
46#include "core/signalmanager.h"
47
48#include "dialogs/adduserdlg.h"
49
50#include "helpers/support.h"
51
52using namespace LicqQtGui;
53/* TRANSLATOR LicqQtGui::SearchUserDlg */
54
55SearchUserDlg::SearchUserDlg()
56  : ppid(LICQ_PPID),
57    searchTag(0)
58{
59  Support::setWidgetProps(this, "SearchUserDialog");
60  setAttribute(Qt::WA_DeleteOnClose, true);
61  setWindowTitle(tr("Licq - User Search"));
62
63  connect(LicqGui::instance()->signalManager(),
64      SIGNAL(searchResult(ICQEvent*)), SLOT(searchResult(ICQEvent*)));
65
66  QVBoxLayout* lay = new QVBoxLayout(this);
67
68  // pre-search widgets
69  grpParms = new QGroupBox(tr("Search Criteria"));
70  lay->addWidget(grpParms);
71
72  QGridLayout* grp_lay = new QGridLayout(grpParms);
73  grp_lay->setColumnMinimumWidth(3, 10); // column interspacing
74
75  QList<QComboBox*> combos;
76
77  QStringList ages = QStringList()
78    << tr("Unspecified")
79    << "18 - 22"
80    << "23 - 29"
81    << "30 - 39"
82    << "40 - 49"
83    << "50 - 59"
84    << "60+";
85
86  QStringList genders = QStringList()
87    << tr("Unspecified")
88    << tr("Female")
89    << tr("Male");
90
91  QStringList languages;
92  for (int i = 0; i < NUM_LANGUAGES; i++)
93    languages << GetLanguageByIndex(i)->szName;
94
95  QStringList countries;
96  for (int i = 0; i < NUM_COUNTRIES; i++)
97    countries << GetCountryByIndex(i)->szName;
98
99  int row = 0;
100  int column = 0;
101
102  QLabel* label;
103
104#define ADDFULLLINE(name, var) \
105  label = new QLabel(name); \
106  var = new QLineEdit(); \
107  label->setBuddy(var); \
108  grp_lay->addWidget(label, row, 0); \
109  grp_lay->addWidget(var, row++, 2, 1, 5);
110
111#define ADDLINE(name, var) \
112  label = new QLabel(name); \
113  var = new QLineEdit(); \
114  label->setBuddy(var); \
115  grp_lay->addWidget(label, row, column); \
116  grp_lay->addWidget(var, row++, column + 2);
117
118#define ADDCMB(name, var, list) \
119  label = new QLabel(name); \
120  var = new QComboBox(); \
121  label->setBuddy(var); \
122  grp_lay->addWidget(label, row, column); \
123  grp_lay->addWidget(var, row++, column + 2); \
124  (var)->addItems((list)); combos << (var);
125
126  ADDFULLLINE(tr("UIN:"), edtUin);
127  edtUin->setValidator(new QIntValidator(10000,2147483647, edtUin));
128
129  // add stretchable space
130  grp_lay->setRowMinimumHeight(row, 20);
131  grp_lay->setRowStretch(row++, 1);
132
133  ADDLINE(tr("Alias:"), edtNick);
134  ADDLINE(tr("First Name:"), edtFirst);
135  ADDLINE(tr("Last Name:"), edtLast);
136  ADDCMB(tr("Age Range:"), cmbAge, ages);
137  ADDCMB(tr("Gender:"), cmbGender, genders);
138  ADDCMB(tr("Language:"), cmbLanguage, languages);
139
140  column = 4; // start new column
141  row = 2; // skip UIN line
142
143  ADDLINE(tr("City:"), edtCity);
144  ADDLINE(tr("State:"), edtState);
145  ADDCMB(tr("Country:"), cmbCountry, countries);
146  ADDLINE(tr("Company Name:"), edtCoName);
147  ADDLINE(tr("Company Department:"), edtCoDept);
148  ADDLINE(tr("Company Position:"), edtCoPos);
149
150  ADDFULLLINE(tr("Email Address:"), edtEmail);
151  ADDFULLLINE(tr("Keyword:"), edtKeyword);
152
153#undef ADDLINE
154#undef ADDFULLLINE
155#undef ADDCMB
156
157  chkOnlineOnly = new QCheckBox(tr("Return Online Users Only"));
158  grp_lay->addWidget(chkOnlineOnly, row++, 0, 1, 7);
159
160  // Don't let comboboxes grow too much.
161  while (!combos.empty())
162    combos.takeFirst()->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
163
164  // post-search widgets
165  grpResult = new QGroupBox(tr("Result"));
166  grp_lay = new QGridLayout(grpResult);
167
168  foundView = new QTreeWidget();
169  QStringList headers = QStringList()
170    << tr("Alias") << tr("UIN") << tr("Name") << tr("Email")
171    << tr("Status") << tr("A/G") << tr("Auth");
172  foundView->setHeaderLabels(headers);
173  foundView->setAllColumnsShowFocus(true);
174  foundView->setSelectionMode(QTreeWidget::ExtendedSelection);
175  foundView->setSortingEnabled(true);
176  foundView->sortByColumn(1, Qt::AscendingOrder);
177  foundView->setIndentation(0);
178  for (int i = 0; i < foundView->columnCount(); i++)
179    foundView->resizeColumnToContents(i);
180  connect(foundView, SIGNAL(itemSelectionChanged()), SLOT(selectionChanged()));
181  grp_lay->addWidget(foundView, 0, 0, 1, 4);
182
183  btnInfo = new QPushButton(tr("View &Info"));
184  btnInfo->setEnabled(false);
185  connect(btnInfo, SIGNAL(clicked()), SLOT(viewInfo()));
186  grp_lay->addWidget(btnInfo, 1, 1);
187
188  btnAdd = new QPushButton(tr("&Add User"));
189  btnAdd->setEnabled(false);
190  connect(btnAdd, SIGNAL(clicked()), SLOT(addUser()));
191  grp_lay->addWidget(btnAdd, 1, 2);
192
193  grp_lay->setRowStretch(0, 1);
194  grp_lay->setColumnStretch(0, 1);
195  lay->addWidget(grpResult, 1);
196
197  // search-control widgets
198  QDialogButtonBox* buttons = new QDialogButtonBox();
199
200  btnSearch = new QPushButton(tr("&Search"), this);
201  btnSearch->setDefault(true);
202  buttons->addButton(btnSearch, QDialogButtonBox::ActionRole);
203  connect(btnSearch, SIGNAL(clicked()), SLOT(startSearch()));
204
205  btnReset = new QPushButton(tr("Reset Search"), this);
206  buttons->addButton(btnReset, QDialogButtonBox::ResetRole);
207  // dirty hack, needed to ensure the text fits into the button...
208  btnReset->setMinimumWidth(btnReset->fontMetrics().width(btnReset->text()) + 20);
209  connect(btnReset, SIGNAL(clicked()), SLOT(resetSearch()));
210
211  btnDone = new QPushButton(tr("Close"), this);
212  buttons->addButton(btnDone, QDialogButtonBox::RejectRole);
213  connect(btnDone, SIGNAL(clicked()), SLOT(close()));
214
215  lay->addWidget(buttons);
216
217  // pseudo Status Bar
218  lblSearch = new QLabel(tr("Enter search parameters and select 'Search'"));
219  lblSearch->setFrameStyle(QLabel::StyledPanel | QLabel::Sunken);
220  lay->addWidget(lblSearch);
221
222  resetSearch();
223
224  show();
225}
226
227void SearchUserDlg::startSearch()
228{
229  unsigned short mins[7] = {0, 18, 23, 30, 40, 50, 60};
230  unsigned short maxs[7] = {0, 22, 29, 39, 49, 59, 120};
231
232  foundView->clear();
233  for (int i = 0; i < foundView->columnCount(); i++)
234    foundView->resizeColumnToContents(i);
235
236  grpParms->hide();
237  grpResult->show();
238
239  btnSearch->setEnabled(false);
240  btnReset->setText(tr("Cancel"));
241  btnDone->setEnabled(false);
242
243  if (edtUin->text().trimmed().isEmpty())
244  {
245    QTextCodec* codec = QTextCodec::codecForName(gUserManager.DefaultUserEncoding());
246    if (codec == 0)
247      codec = QTextCodec::codecForLocale();
248    searchTag = gLicqDaemon->icqSearchWhitePages(
249        codec->fromUnicode(edtFirst->text()),
250        codec->fromUnicode(edtLast->text()),
251        codec->fromUnicode(edtNick->text()),
252        edtEmail->text().toLocal8Bit().data(),
253        mins[cmbAge->currentIndex()],
254        maxs[cmbAge->currentIndex()],
255        cmbGender->currentIndex(),
256        GetLanguageByIndex(cmbLanguage->currentIndex())->nCode,
257        codec->fromUnicode(edtCity->text()),
258        codec->fromUnicode(edtState->text()),
259        GetCountryByIndex(cmbCountry->currentIndex())->nCode,
260        codec->fromUnicode(edtCoName->text()),
261        codec->fromUnicode(edtCoDept->text()),
262        codec->fromUnicode(edtCoPos->text()),
263        codec->fromUnicode(edtKeyword->text()),
264        chkOnlineOnly->isChecked());
265  }
266  else
267    searchTag = gLicqDaemon->icqSearchByUin(edtUin->text().trimmed().toULong());
268
269  lblSearch->setText(tr("Searching (this can take awhile)..."));
270}
271
272void SearchUserDlg::resetSearch()
273{
274  if (searchTag)
275  {
276    searchTag = 0;
277    btnReset->setText(tr("New Search"));
278    lblSearch->setText(tr("Search interrupted"));
279  }
280  else
281  {
282    if (grpParms->isVisible())
283    {
284      edtUin->clear();
285
286      edtNick->clear();
287      edtFirst->clear();
288      edtLast->clear();
289      cmbAge->setCurrentIndex(0);
290      cmbGender->setCurrentIndex(0);
291      cmbLanguage->setCurrentIndex(0);
292      edtCity->clear();
293      edtState->clear();
294      cmbCountry->setCurrentIndex(0);
295      edtCoName->clear();
296      edtCoDept->clear();
297      edtCoPos->clear();
298      edtEmail->clear();
299      edtKeyword->clear();
300      chkOnlineOnly->setChecked(false);
301    }
302    else
303    {
304      foundView->clear();
305      for (int i = 0; i < foundView->columnCount(); i++)
306        foundView->resizeColumnToContents(i);
307
308      grpResult->hide();
309      grpParms->show();
310
311      btnReset->setText(tr("Reset Search"));
312      lblSearch->setText(tr("Enter search parameters and select 'Search'"));
313    }
314  }
315
316  btnDone->setEnabled(true);
317  btnSearch->setEnabled(true);
318}
319
320void SearchUserDlg::searchResult(ICQEvent* e)
321{
322  if (!e->Equals(searchTag))
323    return;
324
325  btnSearch->setEnabled(true);
326  btnDone->setEnabled(true);
327
328  if (e->SearchAck() != NULL && e->SearchAck()->Uin() != 0)
329    searchFound(e->SearchAck());
330
331  if (e->Result() == EVENT_SUCCESS)
332    searchDone(e->SearchAck());
333  else if (e->Result() != EVENT_ACKED)
334    searchFailed();
335}
336
337void SearchUserDlg::searchFound(const CSearchAck* s)
338{
339  QString text;
340  QTreeWidgetItem* item = new QTreeWidgetItem(foundView);
341  QTextCodec* codec = QTextCodec::codecForName(gUserManager.DefaultUserEncoding());
342  if (codec == NULL)
343    codec = QTextCodec::codecForLocale();
344
345  for (int i = 0; i <= 6; i++)
346  {
347    switch (i)
348    {
349      case 0:
350        item->setData(i, Qt::UserRole, QString::number(s->Uin()));
351        text = codec->toUnicode(s->Alias());
352        break;
353      case 1:
354        item->setTextAlignment(i, Qt::AlignRight);
355        text = QString::number(s->Uin());
356        break;
357      case 2:
358        text = codec->toUnicode(s->FirstName()) + " " + codec->toUnicode(s->LastName());
359        break;
360      case 3:
361        text = s->Email();
362        break;
363      case 4:
364        switch (s->Status())
365        {
366          case SA_OFFLINE:
367            text = tr("Offline");
368            break;
369          case SA_ONLINE:
370            text = tr("Online");
371            break;
372          case SA_DISABLED:
373          default:
374            text = tr("Unknown");
375        }
376        break;
377      case 5:
378        text = (s->Age() ? QString::number(s->Age()) : tr("?")) + "/";
379        switch (s->Gender())
380        {
381          case GENDER_FEMALE:
382            text += tr("F");
383            break;
384          case GENDER_MALE:
385            text += tr("M");
386            break;
387          default:
388            text += tr("?");
389        }
390        break;
391      case 6:
392        text = s->Auth() ? tr("No") : tr("Yes");
393        break;
394    }
395    item->setText(i, text);
396  }
397}
398
399void SearchUserDlg::searchDone(const CSearchAck* sa)
400{
401  if (sa == NULL || sa->More() == 0)
402    lblSearch->setText(tr("Search complete."));
403  else if (sa->More() == ~0UL)
404    lblSearch->setText(tr("More users found. Narrow search."));
405  else
406    lblSearch->setText(tr("%1 more users found. Narrow search.").arg(sa->More()));
407
408  searchTag = 0;
409  for (int i = 0; i < foundView->columnCount(); i++)
410    foundView->resizeColumnToContents(i);
411  btnReset->setText(tr("New Search"));
412}
413
414void SearchUserDlg::searchFailed()
415{
416  searchTag = 0;
417  resetSearch();
418  lblSearch->setText(tr("Search failed."));
419}
420
421void SearchUserDlg::selectionChanged()
422{
423  int count = foundView->selectedItems().size();
424
425  btnInfo->setEnabled(true);
426  btnAdd->setEnabled(true);
427
428  switch (count)
429  {
430    case 0:
431      btnInfo->setEnabled(false);
432      btnAdd->setEnabled(false);
433      // fall through
434    case 1:
435      btnAdd->setText(tr("&Add User"));
436      break;
437    default:
438      btnAdd->setText(tr("&Add %1 Users").arg(count));
439  }
440}
441
442void SearchUserDlg::viewInfo()
443{
444  foreach (QTreeWidgetItem* current, foundView->selectedItems())
445  {
446    QByteArray id = current->data(0, Qt::UserRole).toString().toLatin1();
447
448    if (!gUserManager.IsOnList(id, ppid))
449      gLicqDaemon->AddUserToList(id, ppid, false, true);
450
451    LicqGui::instance()->showInfoDialog(mnuUserGeneral, id, ppid, false, true);
452  }
453}
454
455void SearchUserDlg::addUser()
456{
457  foreach (QTreeWidgetItem* current, foundView->selectedItems())
458  {
459    QString id = current->data(0, Qt::UserRole).toString();
460
461    new AddUserDlg(id, ppid, this);
462  }
463
464  foundView->clearSelection();
465}
466
467void SearchUserDlg::reject()
468{
469  if (searchTag != 0)
470    resetSearch();
471  else
472    QDialog::reject();
473}
Note: See TracBrowser for help on using the browser.