00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qclipboard.h>
00022 #include <qptrlist.h>
00023 #include <qtooltip.h>
00024
00025 #include <kdebug.h>
00026 #include <kaction.h>
00027 #include <kxmlguifactory.h>
00028 #include <kxmlguibuilder.h>
00029 #include <ksystemtray.h>
00030 #include <klocale.h>
00031 #include <kiconeffect.h>
00032 #include <kstandarddirs.h>
00033 #include <kpopupmenu.h>
00034 #include <khelpmenu.h>
00035 #include <kfind.h>
00036 #include <kfinddialog.h>
00037 #include <kkeydialog.h>
00038 #include <kglobalaccel.h>
00039 #include <ksimpleconfig.h>
00040 #include <kwin.h>
00041 #include <kbufferedsocket.h>
00042 #include <kserversocket.h>
00043
00044 #include <libkcal/journal.h>
00045 #include <libkcal/calendarlocal.h>
00046
00047 #include "knotesapp.h"
00048 #include "knote.h"
00049 #include "knotesalarm.h"
00050 #include "knoteconfigdlg.h"
00051 #include "knotesglobalconfig.h"
00052 #include "knoteslegacy.h"
00053 #include "knotesnetrecv.h"
00054
00055 #include "knotes/resourcemanager.h"
00056
00057 using namespace KNetwork;
00058
00059
00060 class KNotesKeyDialog : public KDialogBase
00061 {
00062 public:
00063 KNotesKeyDialog( KGlobalAccel *globals, QWidget *parent, const char* name = 0 )
00064 : KDialogBase( parent, name, true, i18n("Configure Shortcuts"), Default|Ok|Cancel, Ok )
00065 {
00066 m_keyChooser = new KKeyChooser( globals, this );
00067 setMainWidget( m_keyChooser );
00068 connect( this, SIGNAL(defaultClicked()), m_keyChooser, SLOT(allDefault()) );
00069 }
00070
00071 void insert( KActionCollection *actions )
00072 {
00073 m_keyChooser->insert( actions, i18n("Note Actions") );
00074 }
00075
00076 void configure()
00077 {
00078 if ( exec() == Accepted )
00079 m_keyChooser->save();
00080 }
00081
00082 private:
00083 KKeyChooser *m_keyChooser;
00084 };
00085
00086
00087 int KNotesApp::KNoteActionList::compareItems( QPtrCollection::Item s1, QPtrCollection::Item s2 )
00088 {
00089 if ( ((KAction*)s1)->text() == ((KAction*)s2)->text() )
00090 return 0;
00091 return ( ((KAction*)s1)->text() < ((KAction*)s2)->text() ? -1 : 1 );
00092 }
00093
00094
00095 KNotesApp::KNotesApp()
00096 : DCOPObject("KNotesIface"), QLabel( 0, 0, WType_TopLevel ),
00097 m_alarm( 0 ), m_listener( 0 ), m_find( 0 ), m_findPos( 0 )
00098 {
00099 connect( kapp, SIGNAL(lastWindowClosed()), kapp, SLOT(quit()) );
00100
00101 m_noteList.setAutoDelete( true );
00102 m_noteActions.setAutoDelete( true );
00103
00104
00105 KWin::setSystemTrayWindowFor( winId(), qt_xrootwin() );
00106 QToolTip::add( this, i18n( "KNotes: Sticky notes for KDE" ) );
00107 setBackgroundMode( X11ParentRelative );
00108 setPixmap( KSystemTray::loadIcon( "knotes" ) );
00109
00110
00111 KNote::setStyle( KNotesGlobalConfig::style() );
00112
00113
00114 new KAction( i18n("New Note"), "filenew", 0,
00115 this, SLOT(newNote()), actionCollection(), "new_note" );
00116 new KAction( i18n("New Note From Clipboard"), "editpaste", 0,
00117 this, SLOT(newNoteFromClipboard()), actionCollection(), "new_note_clipboard" );
00118 new KAction( i18n("Show All Notes"), "knotes", 0,
00119 this, SLOT(showAllNotes()), actionCollection(), "show_all_notes" );
00120 new KAction( i18n("Hide All Notes"), "fileclose", 0,
00121 this, SLOT(hideAllNotes()), actionCollection(), "hide_all_notes" );
00122 new KHelpMenu( this, kapp->aboutData(), false, actionCollection() );
00123
00124 KStdAction::find( this, SLOT(slotOpenFindDialog()), actionCollection() );
00125 KStdAction::preferences( this, SLOT(slotPreferences()), actionCollection() );
00126 KStdAction::keyBindings( this, SLOT(slotConfigureAccels()), actionCollection() );
00127
00128 KStdAction::quit( this, SLOT(slotQuit()), actionCollection() )->setShortcut( 0 );
00129
00130 setXMLFile( instance()->instanceName() + "appui.rc" );
00131
00132 m_guiBuilder = new KXMLGUIBuilder( this );
00133 m_guiFactory = new KXMLGUIFactory( m_guiBuilder, this );
00134 m_guiFactory->addClient( this );
00135
00136 m_context_menu = static_cast<KPopupMenu*>(m_guiFactory->container( "knotes_context", this ));
00137 m_note_menu = static_cast<KPopupMenu*>(m_guiFactory->container( "notes_menu", this ));
00138
00139
00140 QString xmlFileName = instance()->instanceName() + "ui.rc";
00141 QString filter = QString::fromLatin1( instance()->instanceName() + '/' ) + xmlFileName;
00142 QStringList fileList = instance()->dirs()->findAllResources( "data", filter ) +
00143 instance()->dirs()->findAllResources( "data", xmlFileName );
00144
00145 QString doc;
00146 KXMLGUIClient::findMostRecentXMLFile( fileList, doc );
00147 m_noteGUI.setContent( doc );
00148
00149
00150 m_globalAccel = new KGlobalAccel( this, "global accel" );
00151 m_globalAccel->insert( "global_new_note", i18n("New Note"), "",
00152 ALT+SHIFT+Key_N, ALT+SHIFT+Key_N ,
00153 this, SLOT(newNote()), true, true );
00154 m_globalAccel->insert( "global_new_note_clipboard", i18n("New Note From Clipboard"), "",
00155 ALT+SHIFT+Key_C, ALT+SHIFT+Key_C,
00156 this, SLOT(newNoteFromClipboard()), true, true );
00157 m_globalAccel->insert( "global_hide_all_notes", i18n("Hide All Notes"), "",
00158 ALT+SHIFT+Key_H, ALT+SHIFT+Key_H ,
00159 this, SLOT(hideAllNotes()), true, true );
00160 m_globalAccel->insert( "global_show_all_notes", i18n("Show All Notes"), "",
00161 ALT+SHIFT+Key_S, ALT+SHIFT+Key_S,
00162 this, SLOT(showAllNotes()), true, true );
00163
00164 m_globalAccel->readSettings();
00165
00166 KConfig *config = KGlobal::config();
00167 config->setGroup( "Global Keybindings" );
00168 m_globalAccel->setEnabled( config->readBoolEntry( "Enabled", true ) );
00169
00170 updateGlobalAccels();
00171
00172
00173 KNotesLegacy::cleanUp();
00174
00175
00176 m_manager = new KNotesResourceManager();
00177 connect( m_manager, SIGNAL(sigRegisteredNote( KCal::Journal * )),
00178 this, SLOT(createNote( KCal::Journal * )) );
00179 connect( m_manager, SIGNAL(sigDeregisteredNote( KCal::Journal * )),
00180 this, SLOT(killNote( KCal::Journal * )) );
00181
00182
00183 m_manager->load();
00184
00185
00186 KCal::CalendarLocal calendar( QString::fromLatin1( "UTC" ) );
00187 if ( KNotesLegacy::convert( &calendar ) )
00188 {
00189 KCal::Journal::List notes = calendar.journals();
00190 KCal::Journal::List::ConstIterator it;
00191 for ( it = notes.begin(); it != notes.end(); ++it )
00192 m_manager->addNewNote( *it );
00193
00194 m_manager->save();
00195 }
00196
00197
00198
00199 m_alarm = new KNotesAlarm( m_manager, this );
00200
00201
00202 m_listener = new KServerSocket();
00203 m_listener->setResolutionEnabled( true );
00204 connect( m_listener, SIGNAL(readyAccept()), SLOT(acceptConnection()) );
00205 updateNetworkListener();
00206
00207 if ( m_noteList.count() == 0 && !kapp->isRestored() )
00208 newNote();
00209
00210 updateNoteActions();
00211 }
00212
00213 KNotesApp::~KNotesApp()
00214 {
00215 saveNotes();
00216
00217 blockSignals( true );
00218 m_noteList.clear();
00219 blockSignals( false );
00220
00221 delete m_listener;
00222 delete m_manager;
00223 delete m_guiBuilder;
00224 }
00225
00226 bool KNotesApp::commitData( QSessionManager& )
00227 {
00228 saveConfigs();
00229 return true;
00230 }
00231
00232
00233
00234 QString KNotesApp::newNote( const QString& name, const QString& text )
00235 {
00236
00237 KCal::Journal *journal = new KCal::Journal();
00238
00239
00240 if ( !name.isEmpty() )
00241 journal->setSummary( name );
00242 else
00243 journal->setSummary( KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ) );
00244
00245
00246 journal->setDescription( text );
00247
00248 m_manager->addNewNote( journal );
00249
00250 showNote( journal->uid() );
00251
00252 return journal->uid();
00253 }
00254
00255 QString KNotesApp::newNoteFromClipboard( const QString& name )
00256 {
00257 const QString& text = KApplication::clipboard()->text();
00258 return newNote( name, text );
00259 }
00260
00261 void KNotesApp::hideAllNotes() const
00262 {
00263 QDictIterator<KNote> it( m_noteList );
00264 for ( ; *it; ++it )
00265 (*it)->close();
00266 }
00267
00268 void KNotesApp::showAllNotes() const
00269 {
00270 QDictIterator<KNote> it( m_noteList );
00271 for ( ; *it; ++it )
00272 {
00273 (*it)->show();
00274 (*it)->setFocus();
00275 }
00276 }
00277
00278 void KNotesApp::showNote( const QString& id ) const
00279 {
00280 KNote* note = m_noteList[id];
00281 if ( note )
00282 showNote( note );
00283 else
00284 kdWarning(5500) << "showNote: no note with id: " << id << endl;
00285 }
00286
00287 void KNotesApp::hideNote( const QString& id ) const
00288 {
00289 KNote* note = m_noteList[id];
00290 if ( note )
00291 note->hide();
00292 else
00293 kdWarning(5500) << "hideNote: no note with id: " << id << endl;
00294 }
00295
00296 void KNotesApp::killNote( const QString& id, bool force )
00297 {
00298 KNote* note = m_noteList[id];
00299 if ( note )
00300 note->slotKill( force );
00301 else
00302 kdWarning(5500) << "killNote: no note with id: " << id << endl;
00303 }
00304
00305
00306 void KNotesApp::killNote( const QString& id )
00307 {
00308 killNote( id, false );
00309 }
00310
00311 QMap<QString,QString> KNotesApp::notes() const
00312 {
00313 QMap<QString,QString> notes;
00314 QDictIterator<KNote> it( m_noteList );
00315
00316 for ( ; it.current(); ++it )
00317 notes.insert( it.current()->noteId(), it.current()->name() );
00318
00319 return notes;
00320 }
00321
00322 QString KNotesApp::name( const QString& id ) const
00323 {
00324 KNote* note = m_noteList[id];
00325 if ( note )
00326 return note->name();
00327 else
00328 return QString::null;
00329 }
00330
00331 QString KNotesApp::text( const QString& id ) const
00332 {
00333 KNote* note = m_noteList[id];
00334 if ( note )
00335 return note->text();
00336 else
00337 return QString::null;
00338 }
00339
00340 void KNotesApp::setName( const QString& id, const QString& newName )
00341 {
00342 KNote* note = m_noteList[id];
00343 if ( note )
00344 note->setName( newName );
00345 else
00346 kdWarning(5500) << "setName: no note with id: " << id << endl;
00347 }
00348
00349 void KNotesApp::setText( const QString& id, const QString& newText )
00350 {
00351 KNote* note = m_noteList[id];
00352 if ( note )
00353 note->setText( newText );
00354 else
00355 kdWarning(5500) << "setText: no note with id: " << id << endl;
00356 }
00357
00358 void KNotesApp::sync( const QString& app )
00359 {
00360 QDictIterator<KNote> it( m_noteList );
00361
00362 for ( ; it.current(); ++it )
00363 it.current()->sync( app );
00364 }
00365
00366 bool KNotesApp::isNew( const QString& app, const QString& id ) const
00367 {
00368 KNote* note = m_noteList[id];
00369 if ( note )
00370 return note->isNew( app );
00371 else
00372 return false;
00373 }
00374
00375 bool KNotesApp::isModified( const QString& app, const QString& id ) const
00376 {
00377 KNote* note = m_noteList[id];
00378 if ( note )
00379 return note->isModified( app );
00380 else
00381 return false;
00382 }
00383
00384
00385
00386
00387 void KNotesApp::mousePressEvent( QMouseEvent* e )
00388 {
00389 if ( !rect().contains( e->pos() ) )
00390 return;
00391
00392 switch ( e->button() )
00393 {
00394 case LeftButton:
00395 if ( m_noteList.count() == 1 )
00396 {
00397 QDictIterator<KNote> it( m_noteList );
00398 showNote( it.toFirst() );
00399 }
00400 else if ( m_note_menu->count() > 0 )
00401 m_note_menu->popup( e->globalPos() );
00402 break;
00403 case MidButton:
00404 newNote();
00405 break;
00406 case RightButton:
00407 m_context_menu->popup( e->globalPos() );
00408 default: break;
00409 }
00410 }
00411
00412
00413
00414 void KNotesApp::slotShowNote()
00415 {
00416
00417 showNote( QString::fromUtf8( sender()->name() ) );
00418 }
00419
00420 void KNotesApp::slotWalkThroughNotes()
00421 {
00422
00423 QDictIterator<KNote> it( m_noteList );
00424 KNote *first = it.toFirst();
00425 for ( ; *it; ++it )
00426 if ( (*it)->hasFocus() )
00427 {
00428 if ( ++it )
00429 showNote( *it );
00430 else
00431 showNote( first );
00432 break;
00433 }
00434 }
00435
00436 void KNotesApp::slotOpenFindDialog()
00437 {
00438 KFindDialog findDia( this, "find_dialog" );
00439 findDia.setHasSelection( false );
00440 findDia.setHasCursor( false );
00441 findDia.setSupportsBackwardsFind( false );
00442
00443 if ( findDia.exec() != QDialog::Accepted )
00444 return;
00445
00446 delete m_findPos;
00447 m_findPos = new QDictIterator<KNote>( m_noteList );
00448
00449
00450 delete m_find;
00451 m_find = new KFind( findDia.pattern(), findDia.options(), this );
00452
00453 slotFindNext();
00454 }
00455
00456 void KNotesApp::slotFindNext()
00457 {
00458 if ( **m_findPos )
00459 {
00460 KNote *note = **m_findPos;
00461 ++*m_findPos;
00462 note->find( m_find->pattern(), m_find->options() );
00463 }
00464 else
00465 {
00466 m_find->displayFinalDialog();
00467 delete m_find;
00468 m_find = 0;
00469 delete m_findPos;
00470 m_findPos = 0;
00471 }
00472 }
00473
00474 void KNotesApp::slotPreferences()
00475 {
00476
00477 if ( KNoteConfigDlg::showDialog( "KNotes Default Settings" ) )
00478 return;
00479
00480
00481 KNoteConfigDlg *dialog = new KNoteConfigDlg( 0, i18n("Settings"), this,
00482 "KNotes Settings" );
00483 connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateNetworkListener()) );
00484 connect( dialog, SIGNAL(settingsChanged()), this, SLOT(updateStyle()) );
00485 dialog->show();
00486 }
00487
00488 void KNotesApp::slotConfigureAccels()
00489 {
00490 KNotesKeyDialog keys( m_globalAccel, this );
00491 QDictIterator<KNote> notes( m_noteList );
00492 if ( !m_noteList.isEmpty() )
00493 keys.insert( (*notes)->actionCollection() );
00494 keys.configure();
00495
00496 m_globalAccel->writeSettings();
00497 updateGlobalAccels();
00498
00499
00500 m_noteGUI.setContent(
00501 KXMLGUIFactory::readConfigFile( instance()->instanceName() + "ui.rc", instance() )
00502 );
00503
00504 if ( m_noteList.isEmpty() )
00505 return;
00506
00507 notes.toFirst();
00508 QValueList<KAction *> list = (*notes)->actionCollection()->actions();
00509 for ( QValueList<KAction *>::iterator it = list.begin(); it != list.end(); ++it )
00510 {
00511 notes.toFirst();
00512 for ( ++notes; *notes; ++notes )
00513 {
00514 KAction *toChange = (*notes)->actionCollection()->action( (*it)->name() );
00515 if ( toChange->shortcut() != (*it)->shortcut() )
00516 toChange->setShortcut( (*it)->shortcut() );
00517 }
00518 }
00519 }
00520
00521 void KNotesApp::slotNoteKilled( KCal::Journal *journal )
00522 {
00523 m_manager->deleteNote( journal );
00524 saveNotes();
00525 }
00526
00527 void KNotesApp::slotQuit()
00528 {
00529 QDictIterator<KNote> it( m_noteList );
00530
00531 for ( ; *it; ++it )
00532 if ( (*it)->isModified() )
00533 (*it)->saveData();
00534
00535 saveConfigs();
00536 kapp->quit();
00537 }
00538
00539
00540
00541
00542 void KNotesApp::showNote( KNote* note ) const
00543 {
00544 note->show();
00545 KWin::setCurrentDesktop( KWin::windowInfo( note->winId() ).desktop() );
00546 KWin::forceActiveWindow( note->winId() );
00547 note->setFocus();
00548 }
00549
00550 void KNotesApp::createNote( KCal::Journal *journal )
00551 {
00552 KNote *newNote = new KNote( m_noteGUI, journal, 0, journal->uid().utf8() );
00553 m_noteList.insert( newNote->noteId(), newNote );
00554
00555 connect( newNote, SIGNAL(sigRequestNewNote()), SLOT(newNote()) );
00556 connect( newNote, SIGNAL(sigShowNextNote()), SLOT(slotWalkThroughNotes()) );
00557 connect( newNote, SIGNAL(sigKillNote( KCal::Journal* )),
00558 SLOT(slotNoteKilled( KCal::Journal* )) );
00559 connect( newNote, SIGNAL(sigNameChanged()), SLOT(updateNoteActions()) );
00560 connect( newNote, SIGNAL(sigDataChanged()), SLOT(saveNotes()) );
00561 connect( newNote, SIGNAL(sigColorChanged()), SLOT(updateNoteActions()) );
00562 connect( newNote, SIGNAL(sigFindFinished()), SLOT(slotFindNext()) );
00563
00564
00565 if ( m_alarm )
00566 updateNoteActions();
00567 }
00568
00569 void KNotesApp::killNote( KCal::Journal *journal )
00570 {
00571
00572 m_noteList.remove( journal->uid() );
00573 updateNoteActions();
00574 }
00575
00576 void KNotesApp::acceptConnection()
00577 {
00578
00579 KBufferedSocket *s = static_cast<KBufferedSocket *>(m_listener->accept());
00580 if ( s )
00581 {
00582 KNotesNetworkReceiver *recv = new KNotesNetworkReceiver( s );
00583 connect( recv, SIGNAL(sigNoteReceived( const QString &, const QString & )),
00584 this, SLOT(newNote( const QString &, const QString & )) );
00585 }
00586 }
00587
00588 void KNotesApp::saveNotes()
00589 {
00590 KNotesGlobalConfig::writeConfig();
00591 m_manager->save();
00592 }
00593
00594 void KNotesApp::saveConfigs()
00595 {
00596 QDictIterator<KNote> it( m_noteList );
00597 for ( ; it.current(); ++it )
00598 it.current()->saveConfig();
00599 }
00600
00601 void KNotesApp::updateNoteActions()
00602 {
00603 unplugActionList( "notes" );
00604 m_noteActions.clear();
00605
00606 for ( QDictIterator<KNote> it( m_noteList ); it.current(); ++it )
00607 {
00608 KAction *action = new KAction( it.current()->name().replace("&", "&&"),
00609 KShortcut(), this, SLOT(slotShowNote()),
00610 (QObject *)0,
00611 it.current()->noteId().utf8() );
00612 KIconEffect effect;
00613 QPixmap icon = effect.apply( kapp->miniIcon(), KIconEffect::Colorize, 1,
00614 it.current()->paletteBackgroundColor(), false );
00615 action->setIconSet( icon );
00616 m_noteActions.append( action );
00617 }
00618
00619 m_noteActions.sort();
00620
00621 if ( m_noteActions.isEmpty() )
00622 {
00623 KAction *action = new KAction( i18n("No Notes") );
00624 m_noteActions.append( action );
00625 }
00626
00627 plugActionList( "notes", m_noteActions );
00628 }
00629
00630 void KNotesApp::updateGlobalAccels()
00631 {
00632 if ( m_globalAccel->isEnabled() )
00633 {
00634 KAction *action = actionCollection()->action( "new_note" );
00635 if ( action )
00636 action->setShortcut( m_globalAccel->shortcut( "global_new_note" ) );
00637 action = actionCollection()->action( "new_note_clipboard" );
00638 if ( action )
00639 action->setShortcut( m_globalAccel->shortcut( "global_new_note_clipboard" ) );
00640 action = actionCollection()->action( "hide_all_notes" );
00641 if ( action )
00642 action->setShortcut( m_globalAccel->shortcut( "global_hide_all_notes" ) );
00643 action = actionCollection()->action( "show_all_notes" );
00644 if ( action )
00645 action->setShortcut( m_globalAccel->shortcut( "global_show_all_notes" ) );
00646
00647 m_globalAccel->updateConnections();
00648 }
00649 else
00650 {
00651 KAction *action = actionCollection()->action( "new_note" );
00652 if ( action )
00653 action->setShortcut( 0 );
00654 action = actionCollection()->action( "new_note_clipboard" );
00655 if ( action )
00656 action->setShortcut( 0 );
00657 action = actionCollection()->action( "hide_all_notes" );
00658 if ( action )
00659 action->setShortcut( 0 );
00660 action = actionCollection()->action( "show_all_notes" );
00661 if ( action )
00662 action->setShortcut( 0 );
00663 }
00664 }
00665
00666 void KNotesApp::updateNetworkListener()
00667 {
00668 m_listener->close();
00669
00670 if ( KNotesGlobalConfig::receiveNotes() )
00671 {
00672 m_listener->setAddress( QString::number( KNotesGlobalConfig::port() ) );
00673 m_listener->bind();
00674 m_listener->listen();
00675 }
00676 }
00677
00678 void KNotesApp::updateStyle()
00679 {
00680 KNote::setStyle( KNotesGlobalConfig::style() );
00681
00682 QDictIterator<KNote> it( m_noteList );
00683 for ( ; it.current(); ++it )
00684 QApplication::postEvent( *it, new QEvent( QEvent::LayoutHint ) );
00685 }
00686
00687 #include "knotesapp.moc"