~ubuntu-branches/ubuntu/natty/kadu/natty

« back to all changes in this revision

Viewing changes to modules/single_window/single_window.cpp

  • Committer: Package Import Robot
  • Author(s): Kiszel Kristóf
  • Date: 2010-07-21 15:24:54 UTC
  • mfrom: (0.6.1) (0.5.1) (1.4.1) (22.1.2 maverick)
  • Revision ID: package-import@ubuntu.com-20100721152454-vttqle18lovfudni
Tags: 0.6.5.4.ds1-3ubuntu2
Remove libqt4-webkit-dev from build-depends and add
libqtwebkit-dev for qtwebkit transition

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                                                         *
 
3
 *   This program is free software; you can redistribute it and/or modify  *
 
4
 *   it under the terms of the GNU General Public License as published by  *
 
5
 *   the Free Software Foundation; either version 2 of the License, or     *
 
6
 *   (at your option) any later version.                                   *
 
7
 *                                                                         *
 
8
 ***************************************************************************/
 
9
 
 
10
#ifdef Q_WS_HILDON
 
11
# include <QtGui/QMenu>
 
12
# include <QtGui/QMenuBar>
 
13
#endif
 
14
#include <QtCore/QStringList>
 
15
 
 
16
#include "kadu.h"
 
17
#include "misc.h"
 
18
#include "debug.h"
 
19
#include "hot_key.h"
 
20
#include "config_file.h"
 
21
#include "custom_input.h"
 
22
#include "icons_manager.h"
 
23
#include "single_window.h"
 
24
 
 
25
extern "C" KADU_EXPORT int single_window_init(bool firstLoad)
 
26
{
 
27
        kdebugf();
 
28
 
 
29
        singleWindowManager = new SingleWindowManager();
 
30
        MainConfigurationWindow::registerUiFile(dataPath("kadu/modules/configuration/single_window.ui"),
 
31
                singleWindowManager);
 
32
        kdebugf2();
 
33
 
 
34
        return 0;
 
35
}
 
36
extern "C" KADU_EXPORT void single_window_close()
 
37
{
 
38
        kdebugf();
 
39
 
 
40
        MainConfigurationWindow::unregisterUiFile(dataPath("kadu/modules/configuration/single_window.ui"),
 
41
                singleWindowManager);
 
42
        delete singleWindowManager;
 
43
        singleWindowManager = NULL;
 
44
 
 
45
        kdebugf2();
 
46
}
 
47
 
 
48
SingleWindowManager::SingleWindowManager()
 
49
{
 
50
        singleWindow = new SingleWindow();
 
51
}
 
52
 
 
53
SingleWindowManager::~SingleWindowManager()
 
54
{
 
55
        delete singleWindow;
 
56
}
 
57
 
 
58
void SingleWindowManager::configurationUpdated()
 
59
{
 
60
        int newRosterPos = config_file.readNumEntry("SingleWindow", "RosterPosition", 0);
 
61
        if (singleWindow->rosterPosition() != newRosterPos)
 
62
        {
 
63
                singleWindow->changeRosterPos(newRosterPos);
 
64
        }
 
65
}
 
66
 
 
67
SingleWindow::SingleWindow()
 
68
{
 
69
        split = new QSplitter(Qt::Horizontal, this);
 
70
 
 
71
        tabs = new QTabWidget(this);
 
72
        tabs->setTabsClosable(true);
 
73
 
 
74
        rosterPos = config_file.readNumEntry("SingleWindow", "RosterPosition", 0);
 
75
        if (rosterPos == 0)
 
76
        {
 
77
                split->addWidget(kadu);
 
78
                split->addWidget(tabs);
 
79
        }
 
80
        else
 
81
        {
 
82
                split->addWidget(tabs);
 
83
                split->addWidget(kadu);
 
84
        }
 
85
 
 
86
#ifdef Q_WS_HILDON
 
87
        menuBar()->addMenu(kadu->mainMenu());
 
88
 
 
89
        if (kadu->width() >= 250)
 
90
                kadu->resize(250, kadu->height());
 
91
#endif
 
92
 
 
93
        loadWindowGeometry(this, "SingleWindow", "WindowGeometry", 0, 0, 600, 600);
 
94
 
 
95
        if (rosterPos == 0)
 
96
        {
 
97
                splitSizes.append(kadu->width());
 
98
                splitSizes.append(width() - kadu->width());
 
99
        }
 
100
        else
 
101
        {
 
102
                splitSizes.append(width() - kadu->width());
 
103
                splitSizes.append(kadu->width());
 
104
        }
 
105
        split->setSizes(splitSizes);
 
106
 
 
107
        setWindowTitle(kadu->windowTitle());
 
108
 
 
109
        connect(tabs, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
 
110
        connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(onTabChange(int)));
 
111
 
 
112
        connect(chat_manager, SIGNAL(handleNewChatWidget(ChatWidget *,bool &)),
 
113
                        this, SLOT(onNewChat(ChatWidget *,bool &)));
 
114
        connect(chat_manager, SIGNAL(chatWidgetOpen(ChatWidget *)),
 
115
                        this, SLOT(onOpenChat(ChatWidget *)));
 
116
 
 
117
        connect(kadu, SIGNAL(shown()), this, SLOT(show()));
 
118
        connect(kadu, SIGNAL(hiding()), this, SLOT(hide()));
 
119
        connect(kadu, SIGNAL(keyPressed(QKeyEvent *)), this, SLOT(onkaduKeyPressed(QKeyEvent *)));
 
120
        connect(kadu, SIGNAL(statusPixmapChanged(const QIcon &, const QString &)),
 
121
                this, SLOT(onStatusPixmapChanged(const QIcon &, const QString &)));
 
122
 
 
123
        connect(userlist, SIGNAL(statusChanged(UserListElement, QString, const UserStatus &, bool, bool)),
 
124
                this, SLOT(onStatusChanged(UserListElement)));
 
125
 
 
126
        /* conquer all already open chats ;) */
 
127
        ChatList chats = chat_manager->chats();
 
128
        for (uint i = 0; i < chats.count(); i++)
 
129
        {
 
130
                ChatWidget* chat = chats[i];
 
131
                if (chat->parent())
 
132
                        chat->parent()->deleteLater();
 
133
                else
 
134
                        chat->kaduRestoreGeometry();
 
135
                onOpenChat(chat);
 
136
        }
 
137
 
 
138
        show();
 
139
}
 
140
 
 
141
SingleWindow::~SingleWindow()
 
142
{
 
143
        split->setSizes(splitSizes);
 
144
 
 
145
        saveWindowGeometry(this, "SingleWindow", "WindowGeometry");
 
146
 
 
147
        disconnect(chat_manager, SIGNAL(handleNewChatWidget(ChatWidget *,bool &)),
 
148
                        this, SLOT(onNewChat(ChatWidget *,bool &)));
 
149
        disconnect(chat_manager, SIGNAL(chatWidgetOpen(ChatWidget *)),
 
150
                        this, SLOT(onOpenChat(ChatWidget *)));
 
151
 
 
152
        disconnect(tabs, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
 
153
        disconnect(tabs, SIGNAL(currentChanged(int)), this, SLOT(onTabChange(int)));
 
154
 
 
155
        disconnect(kadu, SIGNAL(shown()), this, SLOT(show()));
 
156
        disconnect(kadu, SIGNAL(hiding()), this, SLOT(hide()));
 
157
        disconnect(kadu, SIGNAL(keyPressed(QKeyEvent *)), this, SLOT(onkaduKeyPressed(QKeyEvent *)));
 
158
        disconnect(kadu, SIGNAL(statusPixmapChanged(const QIcon &, const QString &)),
 
159
                this, SLOT(onStatusPixmapChanged(const QIcon &, const QString &)));
 
160
        disconnect(userlist, SIGNAL(statusChanged(UserListElement, QString, const UserStatus &, bool, bool)),
 
161
                this, SLOT(onStatusChanged(UserListElement)));
 
162
 
 
163
        if (!Kadu::closing())
 
164
        {
 
165
                for (int i = tabs->count()-1; i >= 0; --i)
 
166
                {
 
167
                        ChatWidget* chat = dynamic_cast<ChatWidget *>(tabs->widget(i));
 
168
                        UserListElements users = chat->users()->toUserListElements();
 
169
                        tabs->removeTab(i);
 
170
                        delete chat;
 
171
                        chat_manager->openPendingMsgs(users);
 
172
                }
 
173
        }
 
174
 
 
175
        // reparent kadu
 
176
        kadu->setParent(NULL);
 
177
        loadWindowGeometry(kadu, "General", "Geometry", 0, 50, 205, 465);
 
178
}
 
179
 
 
180
void SingleWindow::changeRosterPos(int newRosterPos)
 
181
{
 
182
        rosterPos = newRosterPos;
 
183
        split->insertWidget(rosterPos, kadu);
 
184
}
 
185
 
 
186
void SingleWindow::onNewChat(ChatWidget *w, bool &handled)
 
187
{
 
188
        handled = true;
 
189
        onOpenChat(w);
 
190
}
 
191
 
 
192
void SingleWindow::onOpenChat(ChatWidget *w)
 
193
{
 
194
        QStringList nicks = w->users()->altNicks();
 
195
        QString title = nicks[0];
 
196
        if (nicks.count() > 1)
 
197
                title.append(", ...");
 
198
 
 
199
        tabs->addTab(w, w->icon(), title);
 
200
        tabs->setCurrentIndex(tabs->count()-1);
 
201
        w->edit()->setFocus();
 
202
 
 
203
        connect(w, SIGNAL(messageReceived(ChatWidget *)),
 
204
                this, SLOT(onNewMessage(ChatWidget *)));
 
205
 
 
206
        connect(w, SIGNAL(keyPressed(QKeyEvent*, ChatWidget*, bool&)),
 
207
                this, SLOT(onChatKeyPressed(QKeyEvent*, ChatWidget*, bool&)));
 
208
}
 
209
 
 
210
void SingleWindow::closeTab(int index)
 
211
{
 
212
        tabs->widget(index)->deleteLater();
 
213
        tabs->removeTab(index);
 
214
}
 
215
 
 
216
void SingleWindow::closeEvent(QCloseEvent *event)
 
217
{
 
218
        event->ignore();
 
219
        hide();
 
220
}
 
221
 
 
222
void SingleWindow::resizeEvent(QResizeEvent *event)
 
223
{
 
224
        QSize newSize = event->size();
 
225
        split->resize(newSize);
 
226
}
 
227
 
 
228
void SingleWindow::closeChatWidget(ChatWidget *w)
 
229
{
 
230
        if (w)
 
231
        {
 
232
                int index = tabs->indexOf(w);
 
233
                if (index >= 0)
 
234
                        closeTab(index);
 
235
        }
 
236
}
 
237
 
 
238
void SingleWindow::onNewMessage(ChatWidget *w)
 
239
{
 
240
        if (w != tabs->currentWidget())
 
241
        {
 
242
                int index = tabs->indexOf(w);
 
243
                tabs->setTabIcon(index, icons_manager->loadIcon("Message"));
 
244
 
 
245
                if (config_file.readBoolEntry("SingleWindow", "NumMessagesInTab", false))
 
246
                {
 
247
                        QString title = tabs->tabText(index);
 
248
                        int pos = title.indexOf(" [");
 
249
                        if (pos > -1)
 
250
                                title.truncate(pos);
 
251
                        title += QString(" [%1]").arg(w->newMessagesCount());
 
252
                        tabs->setTabText(index, title);
 
253
                }
 
254
        }
 
255
        else
 
256
        {
 
257
                w->markAllMessagesRead();
 
258
        }
 
259
}
 
260
 
 
261
void SingleWindow::onTabChange(int index)
 
262
{
 
263
        if (index == -1)
 
264
                return;
 
265
 
 
266
        ChatWidget *w = (ChatWidget *)tabs->widget(index);
 
267
        tabs->setTabIcon(index, w->icon());
 
268
 
 
269
        QString title = tabs->tabText(index);
 
270
        int pos = title.indexOf(" [");
 
271
        if (pos > -1)
 
272
                title.truncate(pos);
 
273
        tabs->setTabText(index, title);
 
274
 
 
275
        w->markAllMessagesRead();
 
276
}
 
277
 
 
278
void SingleWindow::onkaduKeyPressed(QKeyEvent *e)
 
279
{
 
280
        /* unfortunatelly does not work correctly */
 
281
        if (HotKey::shortCut(e, "ShortCuts", "FocusOnRosterTab"))
 
282
        {
 
283
                ChatWidget *w = (ChatWidget *)tabs->currentWidget();
 
284
                if (w)
 
285
                {
 
286
                        w->edit()->setFocus();
 
287
                }
 
288
        }
 
289
}
 
290
 
 
291
void SingleWindow::onChatKeyPressed(QKeyEvent* e, ChatWidget* w, bool &handled)
 
292
{
 
293
        /* workaround: we're receiving the same key event twice so ignore the duplicate */
 
294
        static int duplicate = 0;
 
295
        if (duplicate++)
 
296
        {
 
297
                duplicate = 0;
 
298
                handled = false;
 
299
                return;
 
300
        }
 
301
 
 
302
        handled = false;
 
303
 
 
304
        if (HotKey::shortCut(e, "ShortCuts", "SwitchTabLeft"))
 
305
        {
 
306
                int index = tabs->currentIndex();
 
307
                if (index > 0)
 
308
                {
 
309
                        tabs->setCurrentIndex(index-1);
 
310
                }
 
311
                handled = true;
 
312
        }
 
313
        else if (HotKey::shortCut(e, "ShortCuts", "SwitchTabRight"))
 
314
        {
 
315
                int index = tabs->currentIndex();
 
316
                if (index < tabs->count())
 
317
                {
 
318
                        tabs->setCurrentIndex(index+1);
 
319
                }
 
320
                handled = true;
 
321
        }
 
322
        else if (HotKey::shortCut(e, "ShortCuts", "HideShowRoster"))
 
323
        {
 
324
                QList<int> sizes = split->sizes();
 
325
                if (sizes[rosterPos] != 0)
 
326
                        sizes[rosterPos] = 0;
 
327
                else
 
328
                        sizes = splitSizes;
 
329
                split->setSizes(sizes);
 
330
                handled = true;
 
331
        }
 
332
        else if (HotKey::shortCut(e, "ShortCuts", "FocusOnRosterTab"))
 
333
        {
 
334
                kadu->userBox()->setFocus();
 
335
                handled = true;
 
336
        }
 
337
}
 
338
 
 
339
void SingleWindow::onStatusPixmapChanged(const QIcon &icon, const QString &iconName)
 
340
{
 
341
        setWindowIcon(icon);
 
342
}
 
343
 
 
344
void SingleWindow::onStatusChanged(UserListElement ule)
 
345
{
 
346
        ChatWidget *chat = chat_manager->findChatWidget(ule);
 
347
        int index = tabs->indexOf(chat);
 
348
        if (index >= 0)
 
349
        {
 
350
                chat->refreshTitle(); // the icon is not refreshed - refresh it
 
351
                tabs->setTabIcon(index, chat->icon());
 
352
        }
 
353
}
 
354
 
 
355
 
 
356
SingleWindowManager *singleWindowManager = NULL;