root/trunk/qt4-gui/src/userevents/usersendfileevent.cpp

Revision 6463, 6.1 kB (checked in by flynd, 4 months ago)

Use a const pointer for user objects that are only fetched for read access. Fixed some places where we changed the user even though we just had a read lock.

Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of Licq, an instant messaging client for UNIX.
4 * Copyright (C) 2000-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 "config.h"
22
23#include "usersendfileevent.h"
24
25#include <QVBoxLayout>
26#include <QHBoxLayout>
27#include <QSplitter>
28#include <QLabel>
29#include <QPushButton>
30#include <QFileInfo>
31#include <QFileDialog>
32#include <QTextCodec>
33#include <QAction>
34
35#ifdef USE_KDE
36#include <KDE/KFileDialog>
37#endif
38
39#include <licq_icqd.h>
40
41#include "core/gui-defines.h"
42#include "core/licqgui.h"
43#include "core/messagebox.h"
44
45#include "dialogs/filedlg.h"
46#include "dialogs/editfilelistdlg.h"
47
48#include "widgets/infofield.h"
49#include "widgets/mledit.h"
50
51#include "usereventtabdlg.h"
52
53using namespace LicqQtGui;
54/* TRANSLATOR LicqQtGui::UserSendFileEvent */
55
56UserSendFileEvent::UserSendFileEvent(QString id, unsigned long ppid, QWidget* parent)
57  : UserSendCommon(FileEvent, id, ppid, parent, "UserSendFileEvent")
58{
59  myMassMessageCheck->setChecked(false);
60  myMassMessageCheck->setEnabled(false);
61  myForeColor->setEnabled(false);
62  myBackColor->setEnabled(false);
63
64  myMainWidget->addWidget(myViewSplitter);
65
66  QHBoxLayout* h_lay = new QHBoxLayout();
67  myMainWidget->addLayout(h_lay);
68  myFileLabel = new QLabel(tr("File(s): "));
69  h_lay->addWidget(myFileLabel);
70
71  myFileEdit = new InfoField(false);
72  myFileEdit->setReadOnly(true);
73  h_lay->addWidget(myFileEdit);
74
75  myBrowseButton = new QPushButton(tr("Browse"));
76  connect(myBrowseButton, SIGNAL(clicked()), SLOT(browseFile()));
77  h_lay->addWidget(myBrowseButton);
78
79  myEditButton = new QPushButton(tr("Edit"));
80  myEditButton->setEnabled(false);
81  connect(myEditButton, SIGNAL(clicked()), SLOT(editFileList()));
82  h_lay->addWidget(myEditButton);
83
84  myBaseTitle += tr(" - File Transfer");
85
86  UserEventTabDlg* tabDlg = LicqGui::instance()->userEventTabDlg();
87  if (tabDlg != NULL && tabDlg->tabIsSelected(this))
88    tabDlg->setWindowTitle(myBaseTitle);
89
90  setWindowTitle(myBaseTitle);
91  myEventTypeGroup->actions().at(FileEvent)->setChecked(true);
92}
93
94UserSendFileEvent::~UserSendFileEvent()
95{
96  // Empty
97}
98
99void UserSendFileEvent::setFile(const QString& file, const QString& description)
100{
101  QFileInfo fileinfo(file);
102  if (fileinfo.exists() && fileinfo.isFile() && fileinfo.isReadable())
103  {
104    myFileEdit->setText(file);
105    setText(description);
106    myFileList.push_back(strdup(file.toLocal8Bit()));
107    myEditButton->setEnabled(true);
108  }
109}
110
111void UserSendFileEvent::addFile(const QString& file)
112{
113  if (myFileList.size() == 0)
114    return;
115
116  myFileList.push_back(strdup(file.toLocal8Bit()));
117
118  myEditButton->setEnabled(true);
119  myFileEdit->setText(QString(tr("%1 Files")).arg(myFileList.size()));
120}
121
122bool UserSendFileEvent::sendDone(ICQEvent* e)
123{
124  if (!e->ExtendedAck() || !e->ExtendedAck()->Accepted())
125  {
126    const ICQUser* u = gUserManager.FetchUser(myUsers.front().c_str(), myPpid, LOCK_R);
127    QString s = !e->ExtendedAck() ?
128      tr("No reason provided") :
129      myCodec->toUnicode(e->ExtendedAck()->Response());
130    QString result = tr("File transfer with %1 refused:\n%2")
131      .arg(u == NULL ?
132          QString(myUsers.front().c_str()) :
133          QString::fromUtf8(u->GetAlias()))
134      .arg(s);
135    if (u != NULL)
136      gUserManager.DropUser(u);
137    InformUser(this, result);
138  }
139  else
140  {
141    const CEventFile* f = dynamic_cast<const CEventFile*>(e->UserEvent());
142    FileDlg* fileDlg = new FileDlg(myUsers.front().c_str(), myPpid);
143    fileDlg->SendFiles(f->FileList(), e->ExtendedAck()->Port());
144  }
145
146  return true;
147}
148
149void UserSendFileEvent::resetSettings()
150{
151  myMessageEdit->clear();
152  myFileEdit->clear();
153  myMessageEdit->setFocus();
154  myFileList.clear();
155  myEditButton->setEnabled(false);
156  massMessageToggled(false);
157}
158
159void UserSendFileEvent::browseFile()
160{
161#ifdef USE_KDE
162  QStringList fl = KFileDialog::getOpenFileNames(KUrl(), QString(), this, tr("Select files to send"));
163#else
164  QStringList fl = QFileDialog::getOpenFileNames(this, tr("Select files to send"));
165#endif
166
167  if (fl.isEmpty())
168    return;
169
170  QStringList::ConstIterator it = fl.begin();
171
172  for(; it != fl.end(); it++)
173    myFileList.push_back(strdup((*it).toLocal8Bit()));
174
175  updateLabel(myFileList.size());
176}
177
178void UserSendFileEvent::editFileList()
179{
180  EditFileListDlg* dlg = new EditFileListDlg(&myFileList);
181
182  connect(dlg, SIGNAL(fileDeleted(unsigned)), SLOT(updateLabel(unsigned)));
183}
184
185void UserSendFileEvent::updateLabel(unsigned count)
186{
187  QString f;
188
189  myEditButton->setEnabled(true);
190
191  switch (count)
192  {
193    case 0:
194      myEditButton->setEnabled(false);
195      f = QString::null;
196      break;
197
198    case 1:
199      f = myFileList.front();
200      break;
201
202    default:
203      f = QString(tr("%1 Files")).arg(count);
204      break;
205  }
206
207  myFileEdit->setText(f);
208}
209
210void UserSendFileEvent::send()
211{
212  // Take care of typing notification now
213  mySendTypingTimer->stop();
214  connect(myMessageEdit, SIGNAL(textChanged()), SLOT(messageTextChanged()));
215  gLicqDaemon->ProtoTypingNotification(myUsers.front().c_str(), myPpid, false, myConvoId);
216
217  if (myFileEdit->text().trimmed().isEmpty())
218  {
219    WarnUser(this, tr("You must specify a file to transfer!"));
220    return;
221  }
222
223  unsigned long icqEventTag;
224  //TODO in daemon
225  icqEventTag = gLicqDaemon->icqFileTransfer(
226      myUsers.front().c_str(),
227      myCodec->fromUnicode(myFileEdit->text()),
228      myCodec->fromUnicode(myMessageEdit->toPlainText()),
229      myFileList,
230      myUrgentCheck->isChecked() ? ICQ_TCPxMSG_URGENT : ICQ_TCPxMSG_NORMAL,
231      mySendServerCheck->isChecked());
232
233  myEventTag.push_back(icqEventTag);
234
235  UserSendCommon::send();
236}
Note: See TracBrowser for help on using the browser.