summaryrefslogtreecommitdiff
path: root/core/pim/todo/mainwindow.cpp
Unidiff
Diffstat (limited to 'core/pim/todo/mainwindow.cpp') (more/less context) (show whitespace changes)
-rw-r--r--core/pim/todo/mainwindow.cpp621
1 files changed, 621 insertions, 0 deletions
diff --git a/core/pim/todo/mainwindow.cpp b/core/pim/todo/mainwindow.cpp
new file mode 100644
index 0000000..a8f4c9c
--- a/dev/null
+++ b/core/pim/todo/mainwindow.cpp
@@ -0,0 +1,621 @@
1/*
2               =. This file is part of the OPIE Project
3             .=l. Copyright (c) 2002 <>
4           .>+-=
5 _;:,     .>    :=|. This program is free software; you can
6.> <`_,   >  .   <= redistribute it and/or modify it under
7:`=1 )Y*s>-.--   : the terms of the GNU General Public
8.="- .-=="i,     .._ License as published by the Free Software
9 - .   .-<_>     .<> Foundation; either version 2 of the License,
10     ._= =}       : or (at your option) any later version.
11    .%`+i>       _;_.
12    .i_,=:_.      -<s. This program is distributed in the hope that
13     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
14    : ..    .:,     . . . without even the implied warranty of
15    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
16  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
17..}^=.=       =       ; Library General Public License for more
18++=   -.     .`     .: details.
19 :     =  ...= . :.=-
20 -.   .:....=;==+<; You should have received a copy of the GNU
21  -_. . .   )=.  = General Public License along with
22    --        :-=` this library; see the file COPYING.LIB.
23 If not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.
26
27*/
28
29#include <qmenubar.h>
30#include <qmessagebox.h>
31#include <qtoolbar.h>
32#include <qpopupmenu.h>
33#include <qwidgetstack.h>
34#include <qaction.h>
35#include <qtimer.h>
36
37#include <qpe/config.h>
38#include <qpe/ir.h>
39#include <qpe/resource.h>
40#include <qpe/qpemessagebox.h>
41
42
43#include "todotemplatemanager.h"
44#include "templateeditor.h"
45#include "todoentryimpl.h"
46#include "tableview.h"
47
48#include "textviewshow.h"
49#include "todoeditor.h"
50#include "mainwindow.h"
51
52
53using namespace Todo;
54
55MainWindow::MainWindow( QWidget* parent,
56 const char* name ) {
57
58 m_syncing = false;
59 m_counter = 0;
60 m_tempManager = new TemplateManager();
61 m_tempManager->load();
62
63 initUI();
64 initConfig();
65 initViews();
66 initActions();
67 initEditor();
68 initShow();
69 initTemplate();
70
71 populateTemplates();
72 raiseCurrentView();
73 QTimer::singleShot(0, this, SLOT(populateCategories() ) );
74}
75void MainWindow::initTemplate() {
76 m_curTempEd = new TemplateEditor( this, templateManager() );
77}
78void MainWindow::initActions() {
79 QAction* a = new QAction( tr("New Task" ), Resource::loadPixmap( "new" ),
80 QString::null, 0, this, 0 );
81 connect(a, SIGNAL( activated() ),
82 this, SLOT( slotNew() ) );
83 a->addTo(m_tool );
84 a->addTo(m_edit );
85
86 a = new QAction( tr("Edit Task"), Resource::loadIconSet( "edit" ),
87 QString::null, 0, this, 0 );
88 connect(a, SIGNAL(activated() ),
89 this, SLOT( slotEdit() ) );
90 a->addTo( m_tool );
91 a->addTo( m_edit );
92 m_editAction = a;
93
94 a = new QAction( QString::null, tr("View Task"), 0, this, 0 );
95 connect(a, SIGNAL( activated() ),
96 this, SLOT( slotShowDetails() ) );
97 a->addTo( m_edit );
98
99 m_edit->insertSeparator();
100
101 a = new QAction( tr("Delete..."), Resource::loadIconSet( "trash" ),
102 QString::null, 0, this, 0 );
103 connect(a, SIGNAL(activated() ),
104 this, SLOT(slotDelete() ) );
105 a->addTo( m_tool );
106 a->addTo( m_edit );
107 m_deleteAction = a;
108
109 a = new QAction( QString::null, tr("Delete all..."), 0, this, 0 );
110 connect(a, SIGNAL( activated() ),
111 this, SLOT( slotDeleteAll() ) );
112 a->addTo(m_edit );
113 m_deleteAllAction = a;
114
115 a = new QAction( QString::null, tr("Delete completed"),
116 0, this, 0 );
117 connect(a, SIGNAL( activated() ),
118 this, SLOT( slotDeleteCompleted() ) );
119 a->addTo(m_edit );
120 a->setEnabled( TRUE );
121 m_deleteCompleteAction = a;
122
123 m_edit->insertSeparator();
124
125 a = new QAction( QString::null, tr("Duplicate"), 0, this, 0 );
126 connect(a, SIGNAL( activated() ),
127 this, SLOT( slotDuplicate() ) );
128 a->addTo(m_edit );
129 m_duplicateAction = a;
130
131 m_edit->insertSeparator();
132
133 if ( Ir::supported() ) {
134 a = new QAction( tr( "Beam" ), Resource::loadPixmap( "beam" ),
135 QString::null, 0, this, 0 );
136 connect( a, SIGNAL( activated() ),
137 this, SLOT( slotBeam() ) );
138 a->addTo( m_edit );
139 a->addTo( m_tool );
140 }
141
142 a = new QAction( tr("Find"), Resource::loadIconSet( "mag" ),
143 QString::null, 0, this, 0 );
144 connect(a, SIGNAL( activated() ),
145 this, SLOT( slotFind() ) );
146 a->addTo( m_options );
147 m_findAction = a;
148
149 m_options->insertSeparator();
150
151 m_completedAction = new QAction( QString::null, tr("Completed tasks"),
152 0, this, 0, TRUE );
153 m_completedAction->addTo( m_options );
154 m_completedAction->setOn( showCompleted() );
155 connect(m_completedAction, SIGNAL( toggled(bool) ),
156 this, SLOT(slotShowCompleted(bool) ) );
157
158 m_showDeadLineAction = new QAction( QString::null, tr("Show Deadline"),
159 0, this, 0, TRUE );
160 m_showDeadLineAction->addTo( m_options );
161 m_showDeadLineAction->setOn( showDeadline() );
162 connect(m_showDeadLineAction, SIGNAL(toggled(bool) ),
163 this, SLOT( slotShowDeadLine( bool ) ) );
164
165 m_options->insertSeparator();
166
167 m_bar->insertItem( tr("Data") ,m_edit );
168 m_bar->insertItem( tr("Category"), m_catMenu );
169 m_bar->insertItem( tr("Options"), m_options );
170
171 /* initialize the view menu */
172 a = new QAction( QString::null, tr("Show over due"),
173 0, this, 0, TRUE );
174 a->addTo( m_view );
175 a->setOn( showOverDue() );
176 connect(a, SIGNAL(toggled(bool)),
177 this, SLOT(slotShowDue(bool) ) );
178 m_view->insertSeparator();
179
180 m_bar->insertItem( tr("View"), m_view );
181
182 /* templates */
183 m_edit->insertItem(tr("New from template"), m_template,
184 -1, 0 );
185
186}
187/* m_curCat from Config */
188void MainWindow::initConfig() {
189 Config config( "todo" );
190 config.setGroup( "View" );
191 m_completed = config.readBoolEntry( "ShowComplete", TRUE );
192 m_curCat = config.readEntry( "Category", QString::null );
193 m_deadline = config.readBoolEntry( "ShowDeadLine", TRUE);
194 m_overdue = config.readBoolEntry("ShowOverDue", TRUE );
195}
196void MainWindow::initUI() {
197 m_stack = new QWidgetStack(this, "main stack");
198 setCentralWidget( m_stack );
199
200 setToolBarsMovable( FALSE );
201
202 m_tool = new QToolBar( this );
203 m_tool->setHorizontalStretchable( TRUE );
204
205 m_bar = new QMenuBar( m_tool );
206
207 /** QPopupMenu */
208 m_edit = new QPopupMenu( this );
209 m_options = new QPopupMenu( this );
210 m_view = new QPopupMenu( this );
211 m_catMenu = new QPopupMenu( this );
212 m_template = new QPopupMenu( this );
213
214 m_catMenu->setCheckable( TRUE );
215 m_template->setCheckable( TRUE );
216
217 connect(m_catMenu, SIGNAL(activated(int) ),
218 this, SLOT(setCategory(int) ) );
219 connect(m_template, SIGNAL(activated(int) ),
220 this, SLOT(slotNewFromTemplate(int) ) );
221}
222void MainWindow::initViews() {
223 TableView* tableView = new TableView( this, this );
224 m_stack->addWidget( tableView, m_counter++ );
225 m_views.append( tableView );
226 m_curView = tableView;
227 connectBase( tableView );
228// tableView->setTodos( begin(), end() ); we call populateCategories
229
230 /* add QString type + QString configname to
231 * the View menu
232 * and subdirs for multiple views
233 */
234}
235void MainWindow::initEditor() {
236 m_curEdit = new Editor();
237}
238void MainWindow::initShow() {
239 m_curShow = new TextViewShow(this);
240 m_stack->addWidget( m_curShow->widget() , m_counter++ );
241}
242MainWindow::~MainWindow() {
243 delete templateManager();
244}
245void MainWindow::connectBase( ViewBase* base) {
246 base->connectShow( this, SLOT(slotShow(int) ) );
247 base->connectEdit( this, SLOT(slotEdit(int) ) );
248 base->connectUpdateSmall( this,
249 SLOT(slotUpate1(int, const Todo::SmallTodo&) ));
250 base->connectUpdateBig( this,
251 SLOT(slotUpate2(int, const OTodo& ) ) );
252 base->connectUpdateView( this, SLOT(slotUpdate3( QWidget* ) ) ) ;
253 base->connectRemove(&m_todoMgr,
254 SLOT(remove(int)) );
255}
256QPopupMenu* MainWindow::contextMenu( int uid ) {
257 QPopupMenu* menu = new QPopupMenu();
258
259 m_editAction->addTo( menu );
260 m_deleteAction->addTo( menu );
261 m_duplicateAction->addTo( menu );
262 menu->insertSeparator();
263
264 return menu;
265}
266QPopupMenu* MainWindow::options() {
267 qWarning("Options");
268 return m_options;
269}
270QPopupMenu* MainWindow::edit() {
271 return m_edit;
272}
273QPopupMenu* MainWindow::view() {
274 return m_view;
275}
276QToolBar* MainWindow::toolbar() {
277 return m_tool;
278}
279OTodoAccess::List::Iterator MainWindow::begin() {
280 return m_todoMgr.begin();
281}
282OTodoAccess::List::Iterator MainWindow::end() {
283 return m_todoMgr.end();
284}
285OTodo MainWindow::event( int uid ) {
286 return m_todoMgr.event( uid );
287}
288bool MainWindow::isSyncing()const {
289 return m_syncing;
290}
291TemplateManager* MainWindow::templateManager() {
292 return m_tempManager;
293}
294Editor* MainWindow::currentEditor() {
295 return m_curEdit;
296}
297TodoShow* MainWindow::currentShow() {
298 return m_curShow;
299}
300void MainWindow::slotReload() {
301 m_todoMgr.reload();
302 currentView()->setTodos( begin(), end() );
303 raiseCurrentView();
304}
305void MainWindow::closeEvent( QCloseEvent* e ) {
306 if (m_stack->visibleWidget() == currentShow()->widget() ) {
307 raiseCurrentView();
308 e->ignore();
309 return;
310 }
311 /*
312 * we should have flushed and now we're still saving
313 * so there is no need to flush
314 */
315 if (m_syncing ) {
316 e->accept();
317 return;
318 }
319 bool quit = false;
320 if ( m_todoMgr.saveAll() ){
321 qWarning("saved");
322 quit = true;
323 }else {
324 if ( QMessageBox::critical( this, tr("Out of space"),
325 tr("Todo was unable\n"
326 "to save your changes.\n"
327 "Free up some space\n"
328 "and try again.\n"
329 "\nQuit Anyway?"),
330 QMessageBox::Yes|QMessageBox::Escape,
331 QMessageBox::No|QMessageBox::Default)
332 != QMessageBox::No ) {
333 e->accept();
334 quit = true;
335 }else
336 e->ignore();
337
338 }
339
340 if (quit ) {
341 Config config( "todo" );
342 config.setGroup( "View" );
343 config.writeEntry( "ShowComplete", showCompleted() );
344 config.writeEntry( "Category", currentCategory() );
345 config.writeEntry( "ShowDeadLine", showDeadline());
346 config.writeEntry( "ShowOverDue", showOverDue() );
347 /* svae templates */
348 templateManager()->save();
349 e->accept();
350 }
351}
352void MainWindow::populateTemplates() {
353 m_template->clear();
354 QStringList list = templateManager()->templates();
355 QStringList::Iterator it;
356 for ( it = list.begin(); it != list.end(); ++it ) {
357 m_template->insertItem( (*it) );
358 }
359}
360/*
361 * slotNewFromTemplate
362 * We use the edit widget to do
363 * the config but we setUid(-1)
364 * to get a new uid
365 */
366/*
367 * first we get the name of the template
368 * then we will use the TemplateManager
369 */
370void MainWindow::slotNewFromTemplate( int id ) {
371 QString name = m_template->text( id );
372
373 OTodo event = templateManager()->templateEvent( name );
374 event = currentEditor()->edit(this,
375 event );
376
377 if ( currentEditor()->accepted() ) {
378 /* assign new todo */
379 event.setUid( -1 );
380 currentView()->addEvent( event );
381 m_todoMgr.add( event );
382
383 populateCategories();
384 }
385}
386void MainWindow::slotNew() {
387 if(m_syncing) {
388 QMessageBox::warning(this, tr("Todo"),
389 tr("Can not edit data, currently syncing"));
390 return;
391 }
392
393
394 OTodo todo = currentEditor()->newTodo( currentCatId(),
395 this );
396
397 if ( currentEditor()->accepted() ) {
398 //todo.assignUid();
399 currentView()->addEvent( todo );
400 m_todoMgr.add( todo );
401
402 // I'm afraid we must call this every time now, otherwise
403 // spend expensive time comparing all these strings...
404 // but only call if we changed something -zecke
405 populateCategories();
406 }
407
408 raiseCurrentView( );
409}
410void MainWindow::slotDuplicate() {
411 if(m_syncing) {
412 QMessageBox::warning(this, tr("Todo"),
413 tr("Can not edit data, currently syncing"));
414 return;
415 }
416 OTodo ev = m_todoMgr.event( currentView()->current() );
417 /* let's generate a new uid */
418 ev.setUid(-1);
419 m_todoMgr.add( ev );
420
421 currentView()->addEvent( ev );
422 raiseCurrentView();
423}
424void MainWindow::slotDelete() {
425 if (!currentView()->current() )
426 return;
427
428 if(m_syncing) {
429 QMessageBox::warning(this, tr("Todo"),
430 tr("Can not edit data, currently syncing"));
431 return;
432 }
433 QString strName = currentView()->currentRepresentation();
434 if (!QPEMessageBox::confirmDelete(this, tr("Todo"), strName ) )
435 return;
436
437 m_todoMgr.remove( currentView()->current() );
438 currentView()->removeEvent( currentView()->current() );
439 raiseCurrentView();
440}
441void MainWindow::slotDeleteAll() {
442 if(m_syncing) {
443 QMessageBox::warning(this, tr("Todo"),
444 tr("Can not edit data, currently syncing"));
445 return;
446 }
447
448 //QString strName = table->text( table->currentRow(), 2 ).left( 30 );
449
450 if ( !QPEMessageBox::confirmDelete( this, tr( "Todo" ), tr("all tasks?") ) )
451 return;
452
453 m_todoMgr.removeAll();
454 currentView()->clear();
455
456 raiseCurrentView();
457}
458void MainWindow::slotDeleteCompleted() {
459 if(m_syncing) {
460 QMessageBox::warning(this, tr("Todo"),
461 tr("Can not edit data, currently syncing"));
462 return;
463 }
464
465 if ( !QPEMessageBox::confirmDelete( this, tr( "Todo" ), tr("all completed tasks?") ) )
466 return;
467
468 m_todoMgr.remove( currentView()->completed() );
469 currentView()->setTodos( begin(), end() );
470}
471void MainWindow::slotFind() {
472
473}
474void MainWindow::slotEdit() {
475 slotEdit( currentView()->current() );
476}
477/*
478 * set the category
479 */
480void MainWindow::setCategory( int c) {
481 if ( c <= 0 ) return;
482
483 qWarning("Iterating over cats %d", c );
484 for ( unsigned int i = 1; i < m_catMenu->count(); i++ )
485 m_catMenu->setItemChecked(i, c == (int)i );
486
487 if (c == 1 ) {
488 m_curCat = QString::null;
489 setCaption( tr("Todo") + " - " + tr("All Categories" ) );
490
491 }else if ( c == (int)m_catMenu->count() - 1 ) {
492 m_curCat = tr("Unfiled");
493 setCaption( tr("Todo") + " - " + tr("Unfiled") );
494 }else {
495 m_curCat = m_todoMgr.categories()[c-2];
496 setCaption( tr("Todo") + " - " + m_curCat );
497 }
498 m_catMenu->setItemChecked( c, true );
499 currentView()->setShowCategory( m_curCat );
500 raiseCurrentView();
501}
502void MainWindow::slotShowDeadLine( bool dead) {
503 m_deadline = dead;
504 currentView()->setShowDeadline( dead );
505}
506void MainWindow::slotShowCompleted( bool show) {
507 m_completed = show;
508 currentView()->setShowCompleted( m_completed );
509}
510bool MainWindow::showOverDue()const {
511 return m_overdue;
512}
513void MainWindow::setDocument( const QString& ) {
514
515}
516void MainWindow::slotBeam() {
517
518}
519void MainWindow::beamDone( Ir* ) {
520
521}
522
523void MainWindow::slotFlush() {
524 m_syncing = FALSE;
525 m_todoMgr.save();
526}
527void MainWindow::slotShowDetails() {
528 slotShow( currentView()->current() );
529}
530/*
531 * populate the Categories
532 * Menu
533 */
534void MainWindow::populateCategories() {
535 if (m_todoMgr.isLoaded() )
536 m_todoMgr.load();
537
538 m_catMenu->clear();
539 int id, rememberId;
540 id = 1;
541 rememberId = 1;
542
543 m_catMenu->insertItem( tr( "All Categories" ), id++ );
544 m_catMenu->insertSeparator();
545 QStringList categories = m_todoMgr.categories();
546 categories.append( tr( "Unfiled" ) );
547 for ( QStringList::Iterator it = categories.begin();
548 it != categories.end(); ++it ) {
549 m_catMenu->insertItem( *it, id );
550 if ( *it == currentCategory() )
551 rememberId = id;
552 ++id;
553 }
554 setCategory( rememberId );
555}
556bool MainWindow::showCompleted()const {
557 return m_completed;
558}
559bool MainWindow::showDeadline()const {
560 return m_deadline;
561}
562QString MainWindow::currentCategory()const {
563 return m_curCat;
564}
565int MainWindow::currentCatId() {
566 return m_todoMgr.catId( m_curCat );
567}
568ViewBase* MainWindow::currentView() {
569 return m_curView;
570}
571void MainWindow::raiseCurrentView() {
572 m_stack->raiseWidget( m_curView->widget() );
573}
574void MainWindow::slotShowDue(bool ov) {
575 m_overdue = ov;
576 currentView()->showOverDue( ov );
577 raiseCurrentView();
578}
579void MainWindow::slotShow( int uid ) {
580 qWarning("slotShow");
581 currentShow()->slotShow( event( uid ) );
582 m_stack->raiseWidget( currentShow()->widget() );
583}
584void MainWindow::slotEdit( int uid ) {
585 if(m_syncing) {
586 QMessageBox::warning(this, tr("Todo"),
587 tr("Can not edit data, currently syncing"));
588 return;
589 }
590
591 OTodo todo = m_todoMgr.event( uid );
592
593 todo = currentEditor()->edit(this, todo );
594
595 /* if completed */
596 if ( currentEditor()->accepted() ) {
597 qWarning("Replacing now" );
598 m_todoMgr.update( todo.uid(), todo );
599 currentView()->replaceEvent( todo );
600 populateCategories();
601 }
602
603 raiseCurrentView();
604}
605/*
606void MainWindow::slotUpdate1( int uid, const SmallTodo& ev) {
607 m_todoMgr.update( uid, ev );
608}
609*/
610void MainWindow::updateTodo( const OTodo& ev) {
611 m_todoMgr.update( ev.uid() , ev );
612}
613/* The view changed it's configuration
614 * update the view menu
615 */
616void MainWindow::slotUpdate3( QWidget* ) {
617
618}
619void MainWindow::updateList() {
620 m_todoMgr.updateList();
621}