root / kadu-core / gui / windows / kadu-window.cpp @ f38188d2
History | View | Annotate | Download (13.5 kB)
| 1 | /*
|
|---|---|
| 2 | * %kadu copyright begin% |
| 3 | * Copyright 2009 Bartlomiej Zimon (uzi18@o2.pl) |
| 4 | * Copyright 2009 Wojciech Treter (juzefwt@gmail.com) |
| 5 | * Copyright 2009 Juzef (juzefwt@tlen.pl) |
| 6 | * Copyright 2009, 2009, 2010 RafaĆ Malinowski (rafal.przemyslaw.malinowski@gmail.com) |
| 7 | * Copyright 2009 Piotr Galiszewski (piotrgaliszewski@gmail.com) |
| 8 | * %kadu copyright end% |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #include <QtGui/QApplication> |
| 25 | #include <QtGui/QCloseEvent> |
| 26 | #include <QtGui/QHBoxLayout> |
| 27 | #include <QtGui/QMenu> |
| 28 | #include <QtGui/QMenuBar> |
| 29 | #include <QtGui/QPushButton> |
| 30 | #include <QtGui/QSplitter> |
| 31 | |
| 32 | #include "accounts/account-manager.h" |
| 33 | #include "buddies/buddy-manager.h" |
| 34 | #include "buddies/buddy-set.h" |
| 35 | #include "buddies/buddy-shared.h" |
| 36 | #include "buddies/model/buddies-model.h" |
| 37 | #include "buddies/filter/anonymous-without-messages-buddy-filter.h" |
| 38 | #include "buddies/filter/group-buddy-filter.h" |
| 39 | #include "chat/type/chat-type-manager.h" |
| 40 | #include "chat/chat-manager.h" |
| 41 | #include "chat/recent-chat-manager.h" |
| 42 | #include "configuration/configuration-file.h" |
| 43 | #include "contacts/contact.h" |
| 44 | #include "contacts/contact-set.h" |
| 45 | #include "core/core.h" |
| 46 | #include "gui/hot-key.h" |
| 47 | #include "gui/actions/action.h" |
| 48 | #include "gui/widgets/buddy-info-panel.h" |
| 49 | #include "gui/widgets/buddies-list-view.h" |
| 50 | #include "gui/widgets/buddies-list-widget.h" |
| 51 | #include "gui/widgets/chat-widget-actions.h" |
| 52 | #include "gui/widgets/chat-widget-manager.h" |
| 53 | #include "gui/widgets/group-tab-bar.h" |
| 54 | #include "gui/widgets/kadu-text-browser.h" |
| 55 | #include "gui/windows/kadu-window-actions.h" |
| 56 | #include "gui/widgets/status-buttons.h" |
| 57 | #include "gui/widgets/status-menu.h" |
| 58 | |
| 59 | #include "misc/misc.h" |
| 60 | #include "debug.h" |
| 61 | #include "icons-manager.h" |
| 62 | |
| 63 | #include "kadu-window.h" |
| 64 | |
| 65 | KaduWindow::KaduWindow(QWidget *parent) : |
| 66 | MainWindow(parent), Docked(false)
|
| 67 | {
|
| 68 | setAttribute(Qt::WA_DeleteOnClose, true);
|
| 69 | |
| 70 | Actions = new KaduWindowActions(this); |
| 71 | |
| 72 | createGui(); |
| 73 | loadToolBarsFromConfig("");
|
| 74 | configurationUpdated(); |
| 75 | |
| 76 | loadWindowGeometry(this, "General", "Geometry", 0, 50, 205, 465); |
| 77 | } |
| 78 | |
| 79 | KaduWindow::~KaduWindow() |
| 80 | {
|
| 81 | storeConfiguration(); |
| 82 | } |
| 83 | |
| 84 | void KaduWindow::createGui()
|
| 85 | {
|
| 86 | createMenu(); |
| 87 | |
| 88 | MainWidget = new QWidget();
|
| 89 | MainLayout = new QVBoxLayout(MainWidget);
|
| 90 | MainLayout->setMargin(0);
|
| 91 | MainLayout->setSpacing(0);
|
| 92 | |
| 93 | QSplitter *split = new QSplitter(Qt::Vertical, this); |
| 94 | MainLayout->addWidget(split); |
| 95 | |
| 96 | QWidget* hbox = new QWidget(split);
|
| 97 | QHBoxLayout *hboxLayout = new QHBoxLayout(hbox);
|
| 98 | hboxLayout->setMargin(0);
|
| 99 | hboxLayout->setSpacing(0);
|
| 100 | |
| 101 | // groupbar
|
| 102 | GroupBar = new GroupTabBar(this); |
| 103 | |
| 104 | ContactsWidget = new BuddiesListWidget(BuddiesListWidget::FilterAtTop, this); |
| 105 | ContactsWidget->view()->setModel(new BuddiesModel(BuddyManager::instance(), this)); |
| 106 | ContactsWidget->view()->addFilter(GroupBar->filter()); |
| 107 | AnonymousWithoutMessagesBuddyFilter *anonymousFilter = new AnonymousWithoutMessagesBuddyFilter(this); |
| 108 | anonymousFilter->setEnabled(true);
|
| 109 | ContactsWidget->view()->addFilter(anonymousFilter); |
| 110 | |
| 111 | connect(ContactsWidget->view(), SIGNAL(chatActivated(Chat )), this, SLOT(openChatWindow(Chat )));
|
| 112 | |
| 113 | hboxLayout->addWidget(GroupBar); |
| 114 | hboxLayout->setStretchFactor(GroupBar, 1);
|
| 115 | hboxLayout->addWidget(ContactsWidget); |
| 116 | hboxLayout->setStretchFactor(ContactsWidget, 100);
|
| 117 | hboxLayout->setAlignment(GroupBar, Qt::AlignTop); |
| 118 | |
| 119 | InfoPanel = new BuddyInfoPanel(split);
|
| 120 | connect(ContactsWidget->view(), SIGNAL(currentBuddyChanged(Buddy)), InfoPanel, SLOT(displayBuddy(Buddy))); |
| 121 | |
| 122 | if (!config_file.readBoolEntry("Look", "ShowInfoPanel")) |
| 123 | InfoPanel->QWidget::hide(); |
| 124 | |
| 125 | ChangeStatusButtons = new StatusButtons(this); |
| 126 | MainLayout->addWidget(ChangeStatusButtons); |
| 127 | |
| 128 | if (!config_file.readBoolEntry("Look", "ShowStatusButton")) |
| 129 | ChangeStatusButtons->hide(); |
| 130 | |
| 131 | QList<int> splitSizes;
|
| 132 | |
| 133 | splitSizes.append(config_file.readNumEntry("General", "UserBoxHeight")); |
| 134 | splitSizes.append(config_file.readNumEntry("General", "DescriptionHeight")); |
| 135 | |
| 136 | split->setSizes(splitSizes); |
| 137 | |
| 138 | setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 139 | setCentralWidget(MainWidget); |
| 140 | |
| 141 | #ifdef Q_OS_MAC
|
| 142 | qt_mac_set_dock_menu(dockMenu); |
| 143 | #endif
|
| 144 | } |
| 145 | |
| 146 | void KaduWindow::createMenu()
|
| 147 | {
|
| 148 | createKaduMenu(); |
| 149 | createContactsMenu(); |
| 150 | createHelpMenu(); |
| 151 | } |
| 152 | |
| 153 | void KaduWindow::createKaduMenu()
|
| 154 | {
|
| 155 | KaduMenu = new QMenu();
|
| 156 | KaduMenu->setTitle("&Kadu");
|
| 157 | |
| 158 | RecentChatsMenu = new QMenu();
|
| 159 | RecentChatsMenu->setIcon(IconsManager::instance()->loadIcon("OpenChat"));
|
| 160 | RecentChatsMenu->setTitle(tr("Recent chats..."));
|
| 161 | connect(KaduMenu, SIGNAL(aboutToShow()), this, SLOT(createRecentChatsMenu()));
|
| 162 | connect(RecentChatsMenu, SIGNAL(triggered(QAction *)), this, SLOT(openRecentChats(QAction *)));
|
| 163 | |
| 164 | insertMenuActionDescription(Actions->Configuration, MenuKadu); |
| 165 | insertMenuActionDescription(Actions->ShowYourAccounts, MenuKadu); |
| 166 | insertMenuActionDescription(Actions->ManageModules, MenuKadu); |
| 167 | |
| 168 | KaduMenu->addSeparator(); |
| 169 | RecentChatsMenuAction = KaduMenu->addMenu(RecentChatsMenu); |
| 170 | KaduMenu->addSeparator(); |
| 171 | |
| 172 | insertMenuActionDescription(Actions->ExitKadu, MenuKadu); |
| 173 | |
| 174 | menuBar()->addMenu(KaduMenu); |
| 175 | } |
| 176 | |
| 177 | void KaduWindow::createContactsMenu()
|
| 178 | {
|
| 179 | ContactsMenu = new QMenu();
|
| 180 | ContactsMenu->setTitle(tr("&Buddies"));
|
| 181 | |
| 182 | insertMenuActionDescription(Actions->AddUser, MenuContacts); |
| 183 | insertMenuActionDescription(Actions->AddGroup, MenuContacts); |
| 184 | insertMenuActionDescription(Actions->OpenSearch, MenuContacts); |
| 185 | |
| 186 | ContactsMenu->addSeparator(); |
| 187 | // insertMenuActionDescription(ChatWidgetManager::instance()->actions()->openChatWith(), MenuContacts);
|
| 188 | ContactsMenu->addSeparator(); |
| 189 | |
| 190 | insertMenuActionDescription(Actions->ManageIgnored, MenuContacts); |
| 191 | // insertMenuActionDescription(Actions->ImportExportContacts, MenuContacts);
|
| 192 | |
| 193 | menuBar()->addMenu(ContactsMenu); |
| 194 | } |
| 195 | |
| 196 | void KaduWindow::createHelpMenu()
|
| 197 | {
|
| 198 | HelpMenu = new QMenu();
|
| 199 | HelpMenu->setTitle(tr("&Help"));
|
| 200 | |
| 201 | insertMenuActionDescription(Actions->Help, MenuHelp); |
| 202 | HelpMenu->addSeparator(); |
| 203 | insertMenuActionDescription(Actions->Bugs, MenuHelp); |
| 204 | insertMenuActionDescription(Actions->Support, MenuHelp); |
| 205 | insertMenuActionDescription(Actions->GetInvolved, MenuHelp); |
| 206 | HelpMenu->addSeparator(); |
| 207 | insertMenuActionDescription(Actions->About, MenuHelp); |
| 208 | |
| 209 | menuBar()->addMenu(HelpMenu); |
| 210 | } |
| 211 | |
| 212 | void KaduWindow::openChatWindow(Chat chat)
|
| 213 | {
|
| 214 | if (!chat.contacts().toBuddySet().contains(Core::instance()->myself()))
|
| 215 | {
|
| 216 | ChatWidgetManager::instance()->sendMessage(chat); |
| 217 | return;
|
| 218 | } |
| 219 | |
| 220 | // TODO: 0.6.6
|
| 221 | // contact = *contacts.begin();
|
| 222 | // if (contact.mobile().isEmpty() && !contact.email().isEmpty())
|
| 223 | // openMailClient(contact.email());
|
| 224 | } |
| 225 | |
| 226 | void KaduWindow::createRecentChatsMenu()
|
| 227 | {
|
| 228 | kdebugf(); |
| 229 | |
| 230 | RecentChatsMenu->clear(); |
| 231 | |
| 232 | QList<Chat> recentChats = RecentChatManager::instance()->recentChats(); |
| 233 | bool addedAnyChat = false; |
| 234 | foreach (const Chat chat, recentChats)
|
| 235 | if (!ChatWidgetManager::instance()->byChat(chat))
|
| 236 | {
|
| 237 | ChatType *type = ChatTypeManager::instance()->chatType(chat.type()); |
| 238 | QAction *action = new QAction(type ? type->icon() : QIcon(), chat.name(), this); |
| 239 | action->setData(QVariant::fromValue<Chat>(chat)); |
| 240 | RecentChatsMenu->addAction(action); |
| 241 | |
| 242 | addedAnyChat = true;
|
| 243 | } |
| 244 | |
| 245 | RecentChatsMenuAction->setEnabled(addedAnyChat); |
| 246 | |
| 247 | kdebugf2(); |
| 248 | } |
| 249 | |
| 250 | void KaduWindow::openRecentChats(QAction *action)
|
| 251 | {
|
| 252 | kdebugf(); |
| 253 | ChatWidgetManager::instance()->openPendingMsgs(qvariant_cast<Chat>(action->data()), true);
|
| 254 | kdebugf2(); |
| 255 | } |
| 256 | |
| 257 | void KaduWindow::storeConfiguration()
|
| 258 | {
|
| 259 | writeToolBarsToConfig("");
|
| 260 | saveWindowGeometry(this, "General", "Geometry"); |
| 261 | |
| 262 | if (config_file.readBoolEntry("Look", "ShowInfoPanel")) |
| 263 | {
|
| 264 | config_file.writeEntry("General", "UserBoxHeight", ContactsWidget->size().height()); |
| 265 | config_file.writeEntry("General", "DescriptionHeight", InfoPanel->size().height()); |
| 266 | } |
| 267 | if (config_file.readBoolEntry("Look", "ShowStatusButton")) |
| 268 | config_file.writeEntry("General", "UserBoxHeight", ContactsWidget->size().height()); |
| 269 | |
| 270 | // TODO: 0.6.6
|
| 271 | // config_file.writeEntry("General", "DefaultDescription", defaultdescriptions.join("<-->"));
|
| 272 | } |
| 273 | |
| 274 | void KaduWindow::updateInformationPanel()
|
| 275 | {
|
| 276 | InfoPanel->displayBuddy(ContactsWidget->view()->currentContact().ownerBuddy()); |
| 277 | } |
| 278 | |
| 279 | void KaduWindow::closeEvent(QCloseEvent *e)
|
| 280 | {
|
| 281 | e->ignore(); |
| 282 | |
| 283 | if (Docked)
|
| 284 | hide(); |
| 285 | else
|
| 286 | qApp->quit(); |
| 287 | } |
| 288 | |
| 289 | void KaduWindow::customEvent(QEvent *e)
|
| 290 | {
|
| 291 | // TODO: 0.6.6
|
| 292 | // Account defaultAccount = AccountManager::instance()->defaultAccount();
|
| 293 | //
|
| 294 | // if (int(e->type()) == 4321)
|
| 295 | // show();
|
| 296 | // else if (int(e->type()) == 5432)
|
| 297 | // {
|
| 298 | // OpenGGChatEvent *ev = static_cast<OpenGGChatEvent *>(e);
|
| 299 | // if (ev->number() > 0)
|
| 300 | // {
|
| 301 | // Contact contact = userlist->byID("Gadu", QString::number(ev->number())).toContact(defaultAccount);
|
| 302 | // ContactList contacts;
|
| 303 | // contacts << contact;
|
| 304 | // chat_manager->openChatWidget(defaultAccount, contacts, true);
|
| 305 | // }
|
| 306 | // }
|
| 307 | // else
|
| 308 | // KaduMainWIndow::customEvent(e);
|
| 309 | } |
| 310 | |
| 311 | void KaduWindow::keyPressEvent(QKeyEvent *e)
|
| 312 | {
|
| 313 | if (e->key() == Qt::Key_Escape)
|
| 314 | {
|
| 315 | if (Docked)
|
| 316 | {
|
| 317 | kdebugm(KDEBUG_INFO, "Kadu::keyPressEvent(Key_Escape): Kadu hide\n");
|
| 318 | hide(); |
| 319 | } |
| 320 | } |
| 321 | // TODO: 0.6.6 THIS SUXX
|
| 322 | // after action moving this could be restored
|
| 323 | // else if (HotKey::shortCut(e,"ShortCuts", "kadu_deleteuser"))
|
| 324 | // deleteUsersActionDescription->createAction(this)->trigger();
|
| 325 | else if (e->key() == Qt::Key_C && e->modifiers() & Qt::ControlModifier) |
| 326 | InfoPanel->pageAction(QWebPage::Copy)->trigger(); |
| 327 | |
| 328 | emit keyPressed(e); |
| 329 | |
| 330 | MainWindow::keyPressEvent(e); |
| 331 | } |
| 332 | |
| 333 | bool KaduWindow::supportsActionType(ActionDescription::ActionType type)
|
| 334 | {
|
| 335 | return type & (ActionDescription::TypeGlobal | ActionDescription::TypeUserList | ActionDescription::TypeUser);
|
| 336 | } |
| 337 | |
| 338 | BuddiesListView * KaduWindow::contactsListView() |
| 339 | {
|
| 340 | return ContactsWidget->view();
|
| 341 | } |
| 342 | |
| 343 | StatusContainer * KaduWindow::statusContainer() |
| 344 | {
|
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | ContactSet KaduWindow::contacts() |
| 349 | {
|
| 350 | return ContactsWidget->view()->selectedContacts();
|
| 351 | } |
| 352 | |
| 353 | BuddySet KaduWindow::buddies() |
| 354 | {
|
| 355 | return ContactsWidget->view()->selectedBuddies();
|
| 356 | } |
| 357 | |
| 358 | Chat KaduWindow::chat() |
| 359 | {
|
| 360 | return ContactsWidget->view()->currentChat();
|
| 361 | } |
| 362 | |
| 363 | void KaduWindow::configurationUpdated()
|
| 364 | {
|
| 365 | QFont userboxFont = QFont(config_file.readFontEntry("Look", "UserboxFont")); |
| 366 | GroupBar->setFont(QFont(userboxFont.family(), userboxFont.pointSize(), 75));
|
| 367 | |
| 368 | InfoPanel->setVisible(config_file.readBoolEntry("Look", "ShowInfoPanel")); |
| 369 | setDocked(Docked); |
| 370 | |
| 371 | if (config_file.readBoolEntry("Look", "UseUserboxBackground", true)) |
| 372 | {
|
| 373 | QString type = config_file.readEntry("Look", "UserboxBackgroundDisplayStyle"); |
| 374 | ContactsWidget->view()->setBackground(config_file.readColorEntry("Look","UserboxBgColor").name(), |
| 375 | config_file.readEntry("Look", "UserboxBackground"), |
| 376 | type == "Centered" ? BuddiesListView::BackgroundCentered
|
| 377 | : type == "Tiled" ? BuddiesListView::BackgroundTiled
|
| 378 | : type == "Stretched" ? BuddiesListView::BackgroundStretched
|
| 379 | : type == "TiledAndCentered" ? BuddiesListView::BackgroundTiledAndCentered
|
| 380 | : BuddiesListView::BackgroundNone); |
| 381 | } |
| 382 | else
|
| 383 | ContactsWidget->view()->setBackground(config_file.readColorEntry("Look","UserboxBgColor").name()); |
| 384 | |
| 385 | ChangeStatusButtons->setVisible(config_file.readBoolEntry("Look", "ShowStatusButton")); |
| 386 | } |
| 387 | |
| 388 | void KaduWindow::insertMenuActionDescription(ActionDescription *actionDescription, MenuType type, int pos) |
| 389 | {
|
| 390 | kdebugf(); |
| 391 | if (!actionDescription)
|
| 392 | return;
|
| 393 | Action *action = actionDescription->createAction(this);
|
| 394 | |
| 395 | QMenu *menu; |
| 396 | |
| 397 | switch (type)
|
| 398 | {
|
| 399 | case MenuKadu:
|
| 400 | menu = KaduMenu; |
| 401 | break;
|
| 402 | case MenuContacts:
|
| 403 | menu = ContactsMenu; |
| 404 | break;
|
| 405 | case MenuHelp:
|
| 406 | menu = HelpMenu; |
| 407 | } |
| 408 | |
| 409 | QList<QAction *> menuActions = menu->actions(); |
| 410 | if (pos >= menuActions.count() - 1 || pos == -1) |
| 411 | menu->addAction(action); |
| 412 | else
|
| 413 | menu->insertAction(menuActions[pos], action); |
| 414 | |
| 415 | MenuActions[actionDescription] = MenuAction(action, type); |
| 416 | } |
| 417 | |
| 418 | void KaduWindow::removeMenuActionDescription(ActionDescription *actionDescription)
|
| 419 | {
|
| 420 | if (!actionDescription)
|
| 421 | return;
|
| 422 | Action *action = MenuActions[actionDescription].first; |
| 423 | |
| 424 | if (!action)
|
| 425 | return;
|
| 426 | switch (MenuActions[actionDescription].second)
|
| 427 | {
|
| 428 | case MenuKadu:
|
| 429 | KaduMenu->removeAction(action); |
| 430 | break;
|
| 431 | case MenuContacts:
|
| 432 | ContactsMenu->removeAction(action); |
| 433 | break;
|
| 434 | case MenuHelp:
|
| 435 | HelpMenu->removeAction(action); |
| 436 | } |
| 437 | MenuActions.remove(actionDescription); |
| 438 | } |
| 439 | |
| 440 | void KaduWindow::createDefaultToolbars(QDomElement parentConfig)
|
| 441 | {
|
| 442 | QDomElement dockAreaConfig = getDockAreaConfigElement(parentConfig, "topDockArea");
|
| 443 | QDomElement toolbarConfig = xml_config_file->createElement(dockAreaConfig, "ToolBar");
|
| 444 | |
| 445 | addToolButton(toolbarConfig, "addUserAction", Qt::ToolButtonTextUnderIcon);
|
| 446 | addToolButton(toolbarConfig, "addGroupAction", Qt::ToolButtonTextUnderIcon);
|
| 447 | addToolButton(toolbarConfig, "muteSoundsAction", Qt::ToolButtonTextUnderIcon);
|
| 448 | } |
| 449 | |
| 450 | void KaduWindow::addAction(const QString &actionName, Qt::ToolButtonStyle style) |
| 451 | {
|
| 452 | addToolButton(findExistingToolbar(""), actionName, style);
|
| 453 | Core::instance()->kaduWindow()->refreshToolBars("");
|
| 454 | } |
| 455 | |
| 456 | void KaduWindow::setDocked(bool docked) |
| 457 | {
|
| 458 | Docked = docked; |
| 459 | qApp->setQuitOnLastWindowClosed(!Docked); |
| 460 | |
| 461 | // TODO: 0.6.6
|
| 462 | // if (config_file.readBoolEntry("General", "ShowAnonymousWithMsgs") || !Docked || dontHideOnClose)
|
| 463 | // {
|
| 464 | // Userbox->removeNegativeFilter(anonymousUsers);
|
| 465 | // Userbox->applyNegativeFilter(anonymousUsersWithoutMessages);
|
| 466 | // }
|
| 467 | // else
|
| 468 | // {
|
| 469 | // Userbox->removeNegativeFilter(anonymousUsersWithoutMessages);
|
| 470 | // Userbox->applyNegativeFilter(anonymousUsers);
|
| 471 | // }
|
| 472 | } |