kmail

kmmainwin.cpp
1 
2 #ifdef HAVE_CONFIG_H
3 #include <config.h>
4 #endif
5 
6 #include "kmmainwin.h"
7 #include "kmmainwidget.h"
8 #include "kstatusbar.h"
9 #include "messagesender.h"
10 #include "progressdialog.h"
11 #include "statusbarprogresswidget.h"
12 #include "accountwizard.h"
13 #include "broadcaststatus.h"
14 #include "accountmanager.h"
15 #include "kmtransport.h"
16 
17 #include <tdeapplication.h>
18 #include <tdelocale.h>
19 #include <kedittoolbar.h>
20 #include <tdeconfig.h>
21 #include <tdemessagebox.h>
22 #include <kstringhandler.h>
23 #include <kdebug.h>
24 #include <ktip.h>
25 
26 #include "kmmainwin.moc"
27 
28 KMMainWin::KMMainWin(TQWidget *)
29  : TDEMainWindow( 0, "kmail-mainwindow#" ),
30  mReallyClose( false )
31 {
32  // Set this to be the group leader for all subdialogs - this means
33  // modal subdialogs will only affect this dialog, not the other windows
34  setWFlags( getWFlags() | WGroupLeader );
35 
36  kapp->ref();
37 
38  (void) new TDEAction( i18n("New &Window"), "window-new", 0,
39  this, TQ_SLOT(slotNewMailReader()),
40  actionCollection(), "new_mail_client" );
41 
42  mKMMainWidget = new KMMainWidget( this, "KMMainWidget", this, actionCollection() );
43  mKMMainWidget->resize( 450, 600 );
44  setCentralWidget(mKMMainWidget);
45  setupStatusBar();
46  if (kmkernel->xmlGuiInstance())
47  setInstance( kmkernel->xmlGuiInstance() );
48 
49  if ( kmkernel->firstInstance() )
50  TQTimer::singleShot( 200, this, TQ_SLOT(slotShowTipOnStart()) );
51 
52  setStandardToolBarMenuEnabled(true);
53 
54  KStdAction::configureToolbars(this, TQ_SLOT(slotEditToolbars()),
55  actionCollection());
56 
57  KStdAction::keyBindings(mKMMainWidget, TQ_SLOT(slotEditKeys()),
58  actionCollection());
59 
60  KStdAction::quit( this, TQ_SLOT(slotQuit()), actionCollection());
61  createGUI( "kmmainwin.rc", false );
62  // Don't use conserveMemory() because this renders dynamic plugging
63  // of actions unusable!
64 
65  mKMMainWidget->setupForwardingActionsList();
66 
67  applyMainWindowSettings(KMKernel::config(), "Main Window");
68 
69  connect( KPIM::BroadcastStatus::instance(), TQ_SIGNAL( statusMsg( const TQString& ) ),
70  this, TQ_SLOT( displayStatusMsg(const TQString&) ) );
71 
72  connect(kmkernel, TQ_SIGNAL(configChanged()),
73  this, TQ_SLOT(slotConfigChanged()));
74 
75  connect(mKMMainWidget, TQ_SIGNAL(captionChangeRequest(const TQString&)),
76  TQ_SLOT(setCaption(const TQString&)) );
77 
78  // Enable mail checks again (see destructor)
79  kmkernel->enableMailCheck();
80 
81  if ( kmkernel->firstStart() )
82  AccountWizard::start( kmkernel, this );
83 }
84 
85 KMMainWin::~KMMainWin()
86 {
87  saveMainWindowSettings(KMKernel::config(), "Main Window");
88  KMKernel::config()->sync();
89  kapp->deref();
90 
91  if ( !kmkernel->haveSystemTrayApplet() ) {
92  // Check if this was the last KMMainWin
93  int not_withdrawn = 0;
94  TQPtrListIterator<TDEMainWindow> it(*TDEMainWindow::memberList);
95  for (it.toFirst(); it.current(); ++it){
96  if ( !it.current()->isHidden() &&
97  it.current()->isTopLevel() &&
98  it.current() != this &&
99  ::tqt_cast<KMMainWin *>( it.current() )
100  )
101  not_withdrawn++;
102  }
103 
104  if ( not_withdrawn == 0 ) {
105  kdDebug(5006) << "Closing last KMMainWin: stopping mail check" << endl;
106  // Running TDEIO jobs prevent kapp from exiting, so we need to kill them
107  // if they are only about checking mail (not important stuff like moving messages)
108  kmkernel->abortMailCheck();
109  kmkernel->acctMgr()->cancelMailCheck();
110  }
111  }
112 }
113 
114 void KMMainWin::displayStatusMsg(const TQString& aText)
115 {
116  if ( !statusBar() || !mLittleProgress) return;
117  int statusWidth = statusBar()->width() - mLittleProgress->width()
118  - fontMetrics().maxWidth();
119  TQString text = KStringHandler::rPixelSqueeze( " " + aText, fontMetrics(),
120  statusWidth );
121 
122  // ### FIXME: We should disable richtext/HTML (to avoid possible denial of service attacks),
123  // but this code would double the size of the satus bar if the user hovers
124  // over an <foo@bar.com>-style email address :-(
125 // text.replace("&", "&amp;");
126 // text.replace("<", "&lt;");
127 // text.replace(">", "&gt;");
128 
129  statusBar()->changeItem(text, mMessageStatusId);
130 }
131 
132 //-----------------------------------------------------------------------------
133 void KMMainWin::slotNewMailReader()
134 {
135  KMMainWin *d;
136 
137  d = new KMMainWin();
138  d->show();
139  d->resize(d->size());
140 }
141 
142 
143 void KMMainWin::slotEditToolbars()
144 {
145  saveMainWindowSettings(KMKernel::config(), "Main Window");
146  KEditToolbar dlg(actionCollection(), "kmmainwin.rc");
147 
148  connect( &dlg, TQ_SIGNAL(newToolbarConfig()),
149  TQ_SLOT(slotUpdateToolbars()) );
150 
151  dlg.exec();
152 }
153 
154 void KMMainWin::slotUpdateToolbars()
155 {
156  // remove dynamically created actions before editing
157  mKMMainWidget->clearFilterActions();
158 
159  createGUI("kmmainwin.rc", false);
160  applyMainWindowSettings(KMKernel::config(), "Main Window");
161 
162  // plug dynamically created actions again
163  mKMMainWidget->initializeFilterActions();
164 }
165 
166 void KMMainWin::setupStatusBar()
167 {
168  mMessageStatusId = 1;
169 
170  /* Create a progress dialog and hide it. */
171  mProgressDialog = new KPIM::ProgressDialog( statusBar(), this );
172  mProgressDialog->hide();
173 
174  mLittleProgress = new StatusbarProgressWidget( mProgressDialog, statusBar() );
175  mLittleProgress->show();
176 
177  statusBar()->addWidget( mLittleProgress, 0 , true );
178  statusBar()->insertItem(i18n(" Initializing..."), 1, 4 );
179  statusBar()->setItemAlignment( 1, AlignLeft | AlignVCenter );
180  statusBar()->addWidget( mKMMainWidget->vacationScriptIndicator(), 1 );
181  mLittleProgress->show();
182 }
183 
185 void KMMainWin::readConfig(void)
186 {
187 }
188 
190 void KMMainWin::writeConfig(void)
191 {
192  mKMMainWidget->writeConfig();
193 }
194 
195 void KMMainWin::slotQuit()
196 {
197  mReallyClose = true;
198  close();
199 }
200 
201 void KMMainWin::slotConfigChanged()
202 {
203  readConfig();
204 }
205 
206 //-----------------------------------------------------------------------------
207 bool KMMainWin::queryClose()
208 {
209  if ( kapp->sessionSaving() )
210  writeConfig();
211 
212  if ( kmkernel->shuttingDown() || kapp->sessionSaving() || mReallyClose )
213  return true;
214  return kmkernel->canQueryClose();
215 }
216 
217 void KMMainWin::slotShowTipOnStart()
218 {
219  KTipDialog::showTip( this );
220 }
221 
222