Fix layout
[kdepim.git] / kmail / kmmainwin.cpp
blobf0f12d105808dbb3b7621fb9582917aeb707ff4b
1 /*
2 * kmail: KDE mail client
3 * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kmmainwin.h"
22 #include "kmmainwidget.h"
23 #include "Libkdepim/ProgressStatusBarWidget"
24 #include "Libkdepim/StatusbarProgressWidget"
25 #include "libkdepim/broadcaststatus.h"
26 #include "util.h"
27 #include "tag/tagactionmanager.h"
29 #include <QTimer>
30 #include <QAction>
31 #include <QStatusBar>
32 #include <QApplication>
34 #include <KConfigGui>
35 #include <QMenuBar>
36 #include <KToggleAction>
37 #include <KConfigGui>
38 #include <KLocalizedString>
39 #include <kedittoolbar.h>
40 #include <kconfig.h>
41 #include <kmessagebox.h>
42 #include <kxmlguifactory.h>
43 #include <kstringhandler.h>
44 #include <kstandardaction.h>
45 #include "kmail_debug.h"
46 #include <ktip.h>
48 #include <QLabel>
50 KMMainWin::KMMainWin(QWidget *)
51 : KXmlGuiWindow(Q_NULLPTR),
52 mReallyClose(false)
54 setObjectName(QStringLiteral("kmail-mainwindow#"));
55 // Set this to be the group leader for all subdialogs - this means
56 // modal subdialogs will only affect this dialog, not the other windows
57 setAttribute(Qt::WA_GroupLeader);
59 resize(700, 500); // The default size
61 mKMMainWidget = new KMMainWidget(this, this, actionCollection());
62 connect(mKMMainWidget, &KMMainWidget::recreateGui, this, &KMMainWin::slotUpdateGui);
63 setCentralWidget(mKMMainWidget);
64 setupStatusBar();
65 if (!kmkernel->xmlGuiInstanceName().isEmpty()) {
66 setComponentName(kmkernel->xmlGuiInstanceName(), i18n("KMail"));
68 setStandardToolBarMenuEnabled(true);
70 KStandardAction::configureToolbars(this, SLOT(slotEditToolbars()),
71 actionCollection());
73 KStandardAction::keyBindings(this, SLOT(slotConfigureShortcuts()),
74 actionCollection());
76 mHideMenuBarAction = KStandardAction::showMenubar(this, SLOT(slotToggleMenubar()), actionCollection());
77 mHideMenuBarAction->setChecked(KMailSettings::self()->showMenuBar());
78 slotToggleMenubar(true);
80 KStandardAction::quit(this, SLOT(slotQuit()), actionCollection());
81 createGUI(QStringLiteral("kmmainwin.rc"));
83 //must be after createGUI, otherwise e.g toolbar settings are not loaded
84 applyMainWindowSettings(KMKernel::self()->config()->group("Main Window"));
86 connect(KPIM::BroadcastStatus::instance(), SIGNAL(statusMsg(QString)),
87 this, SLOT(displayStatusMsg(QString)));
89 connect(mKMMainWidget, SIGNAL(captionChangeRequest(QString)),
90 SLOT(setCaption(QString)));
92 mKMMainWidget->updateQuickSearchLineText();
95 KMMainWin::~KMMainWin()
97 KConfigGroup grp(KMKernel::self()->config()->group("Main Window"));
98 saveMainWindowSettings(grp);
99 KMKernel::self()->config()->sync();
102 KMMainWidget *KMMainWin::mainKMWidget() const
104 return mKMMainWidget;
107 void KMMainWin::displayStatusMsg(const QString &aText)
109 if (!statusBar() || !mProgressBar->littleProgress()) {
110 return;
112 const int statusWidth = statusBar()->width() - mProgressBar->littleProgress()->width()
113 - fontMetrics().maxWidth();
115 const QString text = fontMetrics().elidedText(QLatin1Char(' ') + aText, Qt::ElideRight,
116 statusWidth);
118 // ### FIXME: We should disable richtext/HTML (to avoid possible denial of service attacks),
119 // but this code would double the size of the status bar if the user hovers
120 // over an <foo@bar.com>-style email address :-(
121 // text.replace("&", "&amp;");
122 // text.replace("<", "&lt;");
123 // text.replace(">", "&gt;");
124 mMessageLabel->setText(text);
127 void KMMainWin::slotToggleMenubar(bool dontShowWarning)
129 if (menuBar()) {
130 if (mHideMenuBarAction->isChecked()) {
131 menuBar()->show();
132 } else {
133 if (!dontShowWarning) {
134 const QString accel = mHideMenuBarAction->shortcut().toString();
135 KMessageBox::information(this,
136 i18n("<qt>This will hide the menu bar completely."
137 " You can show it again by typing %1.</qt>", accel),
138 i18n("Hide menu bar"), QStringLiteral("HideMenuBarWarning"));
140 menuBar()->hide();
142 KMailSettings::self()->setShowMenuBar(mHideMenuBarAction->isChecked());
146 void KMMainWin::slotEditToolbars()
148 KConfigGroup grp = KMKernel::self()->config()->group("Main Window");
149 saveMainWindowSettings(grp);
150 KEditToolBar dlg(guiFactory(), this);
151 connect(&dlg, &KEditToolBar::newToolBarConfig, this, &KMMainWin::slotUpdateGui);
153 dlg.exec();
156 void KMMainWin::slotUpdateGui()
158 // remove dynamically created actions before editing
159 mKMMainWidget->clearFilterActions();
160 mKMMainWidget->tagActionManager()->clearActions();
162 createGUI(QStringLiteral("kmmainwin.rc"));
163 applyMainWindowSettings(KMKernel::self()->config()->group("Main Window"));
165 // plug dynamically created actions again
166 mKMMainWidget->initializeFilterActions();
167 mKMMainWidget->tagActionManager()->createActions();
170 void KMMainWin::setupStatusBar()
172 /* Create a progress dialog and hide it. */
173 mProgressBar = new KPIM::ProgressStatusBarWidget(statusBar(), this);
174 mMessageLabel = new QLabel(i18n("Starting..."));
175 mMessageLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
176 statusBar()->addWidget(mMessageLabel);
178 QTimer::singleShot(2000, KPIM::BroadcastStatus::instance(), &KPIM::BroadcastStatus::reset);
180 statusBar()->addPermanentWidget(mKMMainWidget->vacationScriptIndicator());
181 statusBar()->addPermanentWidget(mProgressBar->littleProgress());
184 void KMMainWin::slotQuit()
186 mReallyClose = true;
187 close();
190 //-----------------------------------------------------------------------------
191 bool KMMainWin::restoreDockedState(int n)
193 // Default restore behavior is to show the window once it is restored.
194 // Override this if the main window was hidden in the system tray
195 // when the session was saved.
196 KConfigGroup config(KConfigGui::sessionConfig(), QString::number(n));
197 bool show = !config.readEntry("docked", false);
199 return KMainWindow::restore(n, show);
202 void KMMainWin::saveProperties(KConfigGroup &config)
204 // This is called by the session manager on log-off
205 // Save the shown/hidden status so we can restore to the same state.
206 KMainWindow::saveProperties(config);
207 config.writeEntry("docked", isHidden());
210 bool KMMainWin::queryClose()
212 if (kmkernel->shuttingDown() || qApp->isSavingSession() || mReallyClose) {
213 return true;
215 return kmkernel->canQueryClose();
218 void KMMainWin::slotConfigureShortcuts()
220 if (guiFactory()->configureShortcuts()) {
221 mKMMainWidget->updateQuickSearchLineText();