21 #include <tqclipboard.h>
22 #include <tqptrlist.h>
23 #include <tqtooltip.h>
26 #include <tdeaction.h>
27 #include <kxmlguifactory.h>
28 #include <kxmlguibuilder.h>
29 #include <ksystemtray.h>
30 #include <tdelocale.h>
31 #include <kiconeffect.h>
32 #include <kstandarddirs.h>
33 #include <tdepopupmenu.h>
34 #include <khelpmenu.h>
36 #include <kfinddialog.h>
37 #include <kkeydialog.h>
38 #include <kglobalaccel.h>
39 #include <ksimpleconfig.h>
41 #include <kbufferedsocket.h>
42 #include <kserversocket.h>
44 #include <libkcal/journal.h>
45 #include <libkcal/calendarlocal.h>
47 #include "knotesapp.h"
49 #include "knotesalarm.h"
50 #include "knoteconfigdlg.h"
51 #include "knotesglobalconfig.h"
52 #include "knoteslegacy.h"
53 #include "knotesnetrecv.h"
55 #include "knotes/resourcemanager.h"
57 using namespace KNetwork;
60 class KNotesKeyDialog :
public KDialogBase
63 KNotesKeyDialog( TDEGlobalAccel *globals, TQWidget *parent,
const char* name = 0 )
64 : KDialogBase( parent, name, true, i18n(
"Configure Shortcuts"), Default|Ok|Cancel, Ok )
66 m_keyChooser =
new KKeyChooser( globals,
this );
67 setMainWidget( m_keyChooser );
68 connect(
this, TQT_SIGNAL(defaultClicked()), m_keyChooser, TQT_SLOT(allDefault()) );
71 void insert( TDEActionCollection *actions )
73 m_keyChooser->insert( actions, i18n(
"Note Actions") );
78 if ( exec() == Accepted )
83 KKeyChooser *m_keyChooser;
87 int KNotesApp::KNoteActionList::compareItems( TQPtrCollection::Item s1, TQPtrCollection::Item s2 )
89 if ( ((TDEAction*)s1)->text() == ((TDEAction*)s2)->text() )
91 return ( ((TDEAction*)s1)->text() < ((TDEAction*)s2)->text() ? -1 : 1 );
95 KNotesApp::KNotesApp()
96 : DCOPObject(
"KNotesIface"), TQLabel( 0, 0, WType_TopLevel ),
97 m_alarm( 0 ), m_listener( 0 ), m_find( 0 ), m_findPos( 0 )
99 connect( kapp, TQT_SIGNAL(lastWindowClosed()), kapp, TQT_SLOT(quit()) );
101 m_noteList.setAutoDelete(
true );
102 m_noteActions.setAutoDelete(
true );
105 KWin::setSystemTrayWindowFor( winId(), tqt_xrootwin() );
106 TQToolTip::add(
this, i18n(
"KNotes: Sticky notes for TDE" ) );
107 setBackgroundMode( X11ParentRelative );
108 setPixmap( KSystemTray::loadIcon(
"knotes" ) );
111 KNote::setStyle( KNotesGlobalConfig::style() );
114 new TDEAction( i18n(
"New Note"),
"document-new", 0,
115 TQT_TQOBJECT(
this), TQT_SLOT(newNote()), actionCollection(),
"new_note" );
116 new TDEAction( i18n(
"New Note From Clipboard"),
"edit-paste", 0,
117 TQT_TQOBJECT(
this), TQT_SLOT(newNoteFromClipboard()), actionCollection(),
"new_note_clipboard" );
118 new TDEAction( i18n(
"Show All Notes"),
"knotes", 0,
119 TQT_TQOBJECT(
this), TQT_SLOT(showAllNotes()), actionCollection(),
"show_all_notes" );
120 new TDEAction( i18n(
"Hide All Notes"),
"window-close", 0,
121 TQT_TQOBJECT(
this), TQT_SLOT(hideAllNotes()), actionCollection(),
"hide_all_notes" );
122 new KHelpMenu(
this, kapp->aboutData(),
false, actionCollection() );
124 m_findAction = KStdAction::find( TQT_TQOBJECT(
this), TQT_SLOT(slotOpenFindDialog()), actionCollection() );
125 KStdAction::preferences( TQT_TQOBJECT(
this), TQT_SLOT(slotPreferences()), actionCollection() );
126 KStdAction::keyBindings( TQT_TQOBJECT(
this), TQT_SLOT(slotConfigureAccels()), actionCollection() );
128 KStdAction::quit( TQT_TQOBJECT(
this), TQT_SLOT(slotQuit()), actionCollection() )->setShortcut( 0 );
130 setXMLFile( instance()->instanceName() +
"appui.rc" );
132 m_guiBuilder =
new KXMLGUIBuilder(
this );
133 m_guiFactory =
new KXMLGUIFactory( m_guiBuilder, TQT_TQOBJECT(
this) );
134 m_guiFactory->addClient(
this );
136 m_context_menu =
static_cast<TDEPopupMenu*
>(m_guiFactory->container(
"knotes_context",
this ));
137 m_note_menu =
static_cast<TDEPopupMenu*
>(m_guiFactory->container(
"notes_menu",
this ));
140 TQString xmlFileName = instance()->instanceName() +
"ui.rc";
141 TQString filter = TQString::fromLatin1( instance()->instanceName() +
'/' ) + xmlFileName;
142 TQStringList fileList = instance()->dirs()->findAllResources(
"data", filter ) +
143 instance()->dirs()->findAllResources(
"data", xmlFileName );
146 KXMLGUIClient::findMostRecentXMLFile( fileList, doc );
147 m_noteGUI.setContent( doc );
150 m_globalAccel =
new TDEGlobalAccel( TQT_TQOBJECT(
this),
"global accel" );
151 m_globalAccel->insert(
"global_new_note", i18n(
"New Note"),
"",
152 TDEShortcut(), TDEShortcut(),
153 TQT_TQOBJECT(
this), TQT_SLOT(newNote()),
true,
true );
154 m_globalAccel->insert(
"global_new_note_clipboard", i18n(
"New Note From Clipboard"),
"",
155 TDEShortcut(), TDEShortcut(),
156 TQT_TQOBJECT(
this), TQT_SLOT(newNoteFromClipboard()),
true,
true );
157 m_globalAccel->insert(
"global_hide_all_notes", i18n(
"Hide All Notes"),
"",
158 TDEShortcut(), TDEShortcut(),
159 TQT_TQOBJECT(
this), TQT_SLOT(hideAllNotes()),
true,
true );
160 m_globalAccel->insert(
"global_show_all_notes", i18n(
"Show All Notes"),
"",
161 TDEShortcut(), TDEShortcut(),
162 TQT_TQOBJECT(
this), TQT_SLOT(showAllNotes()),
true,
true );
164 m_globalAccel->readSettings();
166 TDEConfig *config = TDEGlobal::config();
167 config->setGroup(
"Global Keybindings" );
168 m_globalAccel->setEnabled( config->readBoolEntry(
"Enabled",
true ) );
170 updateGlobalAccels();
173 KNotesLegacy::cleanUp();
176 m_manager =
new KNotesResourceManager();
177 connect( m_manager, TQT_SIGNAL(sigRegisteredNote( KCal::Journal * )),
178 this, TQT_SLOT(createNote( KCal::Journal * )) );
179 connect( m_manager, TQT_SIGNAL(sigDeregisteredNote( KCal::Journal * )),
180 this, TQT_SLOT(killNote( KCal::Journal * )) );
186 KCal::CalendarLocal calendar( TQString::fromLatin1(
"UTC" ) );
187 if ( KNotesLegacy::convert( &calendar ) )
189 KCal::Journal::List notes = calendar.journals();
190 KCal::Journal::List::ConstIterator it;
191 for ( it = notes.constBegin(); it != notes.constEnd(); ++it )
192 m_manager->addNewNote( *it );
199 m_alarm =
new KNotesAlarm( m_manager, TQT_TQOBJECT(
this) );
202 m_listener =
new TDEServerSocket();
203 m_listener->setResolutionEnabled(
true );
204 connect( m_listener, TQT_SIGNAL(readyAccept()), TQT_SLOT(acceptConnection()) );
205 updateNetworkListener();
207 if ( m_noteList.count() == 0 && !kapp->isRestored() )
213 void KNotesApp::resizeTrayIcon ()
217 TQPixmap scaledpixmap;
219 origpixmap = KSystemTray::loadSizedIcon(
"knotes", TQWidget::width() );
220 newIcon = origpixmap;
221 newIcon = newIcon.smoothScale(TQWidget::width(), TQWidget::height());
222 scaledpixmap = newIcon;
223 setPixmap(scaledpixmap);
226 void KNotesApp::resizeEvent ( TQResizeEvent * )
232 void KNotesApp::showEvent ( TQShowEvent * )
238 KNotesApp::~KNotesApp()
242 blockSignals(
true );
244 blockSignals(
false );
251 bool KNotesApp::commitData( TQSessionManager& )
259 TQString KNotesApp::newNote(
const TQString& name,
const TQString& text )
262 KCal::Journal *journal =
new KCal::Journal();
265 if ( !name.isEmpty() )
266 journal->setSummary( name );
268 journal->setSummary( TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime() ) );
271 journal->setDescription( text );
273 if ( m_manager->addNewNote( journal ) ) {
274 showNote( journal->uid() );
276 return journal->uid();
279 TQString KNotesApp::newNoteFromClipboard(
const TQString& name )
281 const TQString& text = TDEApplication::clipboard()->text();
282 return newNote( name, text );
285 void KNotesApp::hideAllNotes()
const
287 TQDictIterator<KNote> it( m_noteList );
292 void KNotesApp::showAllNotes()
const
294 TQDictIterator<KNote> it( m_noteList );
301 void KNotesApp::showNote(
const TQString&
id )
const
303 KNote* note = m_noteList[id];
307 kdWarning(5500) <<
"showNote: no note with id: " <<
id << endl;
310 void KNotesApp::hideNote(
const TQString&
id )
const
312 KNote* note = m_noteList[id];
316 kdWarning(5500) <<
"hideNote: no note with id: " <<
id << endl;
319 void KNotesApp::killNote(
const TQString&
id,
bool force )
321 KNote* note = m_noteList[id];
323 note->slotKill( force );
325 kdWarning(5500) <<
"killNote: no note with id: " <<
id << endl;
329 void KNotesApp::killNote(
const TQString&
id )
331 killNote(
id,
false );
334 TQMap<TQString,TQString> KNotesApp::notes()
const
336 TQMap<TQString,TQString> notes;
337 TQDictIterator<KNote> it( m_noteList );
339 for ( ; it.current(); ++it )
340 notes.insert( it.current()->noteId(), it.current()->name() );
345 TQString KNotesApp::name(
const TQString&
id )
const
347 KNote* note = m_noteList[id];
354 TQString KNotesApp::text(
const TQString&
id )
const
356 KNote* note = m_noteList[id];
363 void KNotesApp::setName(
const TQString&
id,
const TQString& newName )
365 KNote* note = m_noteList[id];
367 note->setName( newName );
369 kdWarning(5500) <<
"setName: no note with id: " <<
id << endl;
372 void KNotesApp::setText(
const TQString&
id,
const TQString& newText )
374 KNote* note = m_noteList[id];
376 note->setText( newText );
378 kdWarning(5500) <<
"setText: no note with id: " <<
id << endl;
381 TQString KNotesApp::fgColor(
const TQString&
id )
const
383 KNote* note = m_noteList[id];
385 return note->fgColor().name();
390 TQString KNotesApp::bgColor(
const TQString&
id )
const
392 KNote* note = m_noteList[id];
394 return note->bgColor().name();
399 void KNotesApp::setColor(
const TQString&
id,
const TQString& fgColor,
const TQString& bgColor )
401 KNote* note = m_noteList[id];
403 note->setColor( TQColor( fgColor ), TQColor( bgColor ) );
405 kdWarning(5500) <<
"setColor: no note with id: " <<
id << endl;
408 int KNotesApp::width(
const TQString&
id )
const
410 KNote* note = m_noteList[id];
412 return note->width();
417 int KNotesApp::height(
const TQString&
id )
const
419 KNote* note = m_noteList[id];
421 return note->height();
426 void KNotesApp::move(
const TQString&
id,
int x,
int y )
const
428 KNote* note = m_noteList[id];
430 return note->move( x, y );
432 kdWarning(5500) <<
"move: no note with id: " <<
id << endl;
435 void KNotesApp::resize(
const TQString&
id,
int width,
int height )
const
437 KNote* note = m_noteList[id];
439 return note->resize( width, height );
441 kdWarning(5500) <<
"resize: no note with id: " <<
id << endl;
444 void KNotesApp::sync(
const TQString& app )
446 TQDictIterator<KNote> it( m_noteList );
448 for ( ; it.current(); ++it )
449 it.current()->sync( app );
452 bool KNotesApp::isNew(
const TQString& app,
const TQString&
id )
const
454 KNote* note = m_noteList[id];
456 return note->isNew( app );
461 bool KNotesApp::isModified(
const TQString& app,
const TQString&
id )
const
463 KNote* note = m_noteList[id];
465 return note->isModified( app );
473 void KNotesApp::mousePressEvent( TQMouseEvent* e )
475 if ( !rect().contains( e->pos() ) )
478 switch ( e->button() )
481 if ( m_noteList.count() == 1 )
483 TQDictIterator<KNote> it( m_noteList );
484 showNote( it.toFirst() );
486 else if ( m_note_menu->count() > 0 )
487 m_note_menu->popup( e->globalPos() );
492 case Qt::RightButton:
493 m_context_menu->popup( e->globalPos() );
500 void KNotesApp::slotShowNote()
503 showNote( TQString::fromUtf8( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() ) );
506 void KNotesApp::slotWalkThroughNotes()
509 TQDictIterator<KNote> it( m_noteList );
510 KNote *first = it.toFirst();
512 if ( (*it)->hasFocus() )
522 void KNotesApp::slotOpenFindDialog()
524 KFindDialog findDia(
this,
"find_dialog" );
525 findDia.setHasSelection(
false );
526 findDia.setHasCursor(
false );
527 findDia.setSupportsBackwardsFind(
false );
529 if ( (findDia.exec() != TQDialog::Accepted) || findDia.pattern().isEmpty() )
533 m_findPos =
new TQDictIterator<KNote>( m_noteList );
537 m_find =
new KFind( findDia.pattern(), findDia.options(), this );
542 void KNotesApp::slotFindNext()
546 KNote *note = **m_findPos;
548 note->find( m_find->pattern(), m_find->options() );
552 m_find->displayFinalDialog();
560 void KNotesApp::slotPreferences()
563 if ( KNoteConfigDlg::showDialog(
"KNotes Default Settings" ) )
567 KNoteConfigDlg *dialog =
new KNoteConfigDlg( 0, i18n(
"Settings"),
this,
569 connect( dialog, TQT_SIGNAL(settingsChanged()), TQT_TQOBJECT(
this), TQT_SLOT(updateNetworkListener()) );
570 connect( dialog, TQT_SIGNAL(settingsChanged()), TQT_TQOBJECT(
this), TQT_SLOT(updateStyle()) );
574 void KNotesApp::slotConfigureAccels()
576 KNotesKeyDialog keys( m_globalAccel,
this );
577 TQDictIterator<KNote> notes( m_noteList );
578 if ( !m_noteList.isEmpty() )
579 keys.insert( (*notes)->actionCollection() );
582 m_globalAccel->writeSettings();
583 updateGlobalAccels();
586 m_noteGUI.setContent(
587 KXMLGUIFactory::readConfigFile( instance()->instanceName() +
"ui.rc", instance() )
590 if ( m_noteList.isEmpty() )
594 TQValueList<TDEAction *> list = (*notes)->actionCollection()->actions();
595 for ( TQValueList<TDEAction *>::iterator it = list.begin(); it != list.end(); ++it )
598 for ( ++notes; *notes; ++notes )
600 TDEAction *toChange = (*notes)->actionCollection()->action( (*it)->name() );
601 if ( toChange->shortcut() != (*it)->shortcut() )
602 toChange->setShortcut( (*it)->shortcut() );
607 void KNotesApp::slotNoteKilled( KCal::Journal *journal )
610 m_manager->deleteNote( journal );
614 void KNotesApp::slotQuit()
616 TQDictIterator<KNote> it( m_noteList );
619 if ( (*it)->isModified() )
620 (*it)->saveData(
false);
629 void KNotesApp::showNote( KNote* note )
const
632 KWin::setCurrentDesktop( KWin::windowInfo( note->winId() ).desktop() );
633 KWin::forceActiveWindow( note->winId() );
637 void KNotesApp::createNote( KCal::Journal *journal )
639 if( journal->uid() == m_noteUidModify)
641 KNote *note = m_noteList[m_noteUidModify];
643 note->changeJournal(journal);
647 m_noteUidModify = journal->uid();
648 KNote *newNote =
new KNote( m_noteGUI, journal, 0, journal->uid().utf8() );
649 m_noteList.insert( newNote->noteId(), newNote );
651 connect( newNote, TQT_SIGNAL(sigRequestNewNote()), TQT_SLOT(newNote()) );
652 connect( newNote, TQT_SIGNAL(sigShowNextNote()), TQT_SLOT(slotWalkThroughNotes()) );
653 connect( newNote, TQT_SIGNAL(sigKillNote( KCal::Journal* )),
654 TQT_SLOT(slotNoteKilled( KCal::Journal* )) );
655 connect( newNote, TQT_SIGNAL(sigNameChanged()), TQT_SLOT(updateNoteActions()) );
656 connect( newNote, TQT_SIGNAL(sigDataChanged(
const TQString &)), TQT_SLOT(saveNotes(
const TQString &)) );
657 connect( newNote, TQT_SIGNAL(sigColorChanged()), TQT_SLOT(updateNoteActions()) );
658 connect( newNote, TQT_SIGNAL(sigFindFinished()), TQT_SLOT(slotFindNext()) );
665 void KNotesApp::killNote( KCal::Journal *journal )
667 if(m_noteUidModify == journal->uid())
672 KNote *note = m_noteList.take( journal->uid() );
675 note->deleteWhenIdle();
680 void KNotesApp::acceptConnection()
683 TDEBufferedSocket *s =
static_cast<TDEBufferedSocket *
>(m_listener->accept());
686 KNotesNetworkReceiver *recv =
new KNotesNetworkReceiver( s );
687 connect( recv, TQT_SIGNAL(sigNoteReceived(
const TQString &,
const TQString & )),
688 TQT_TQOBJECT(
this), TQT_SLOT(newNote(
const TQString &,
const TQString & )) );
692 void KNotesApp::saveNotes(
const TQString & uid )
694 m_noteUidModify = uid;
698 void KNotesApp::saveNotes()
700 KNotesGlobalConfig::writeConfig();
704 void KNotesApp::saveConfigs()
706 TQDictIterator<KNote> it( m_noteList );
707 for ( ; it.current(); ++it )
708 it.current()->saveConfig();
711 void KNotesApp::updateNoteActions()
713 unplugActionList(
"notes" );
714 m_noteActions.clear();
716 for ( TQDictIterator<KNote> it( m_noteList ); it.current(); ++it )
718 TDEAction *action =
new TDEAction( it.current()->name().replace(
"&",
"&&"),
719 TDEShortcut(), TQT_TQOBJECT(
this), TQT_SLOT(slotShowNote()),
721 it.current()->noteId().utf8() );
722 TDEIconEffect effect;
723 TQPixmap icon = effect.apply( kapp->miniIcon(), TDEIconEffect::Colorize, 1,
724 it.current()->paletteBackgroundColor(), false );
725 action->setIconSet( icon );
726 m_noteActions.append( action );
729 if ( m_noteActions.isEmpty() )
731 actionCollection()->action(
"hide_all_notes" )->setEnabled(
false );
732 actionCollection()->action(
"show_all_notes" )->setEnabled(
false );
733 m_findAction->setEnabled(
false );
734 TDEAction *action =
new TDEAction( i18n(
"No Notes") );
735 m_noteActions.append( action );
739 actionCollection()->action(
"hide_all_notes" )->setEnabled(
true );
740 actionCollection()->action(
"show_all_notes" )->setEnabled(
true );
741 m_findAction->setEnabled(
true );
742 m_noteActions.sort();
744 plugActionList(
"notes", m_noteActions );
747 void KNotesApp::updateGlobalAccels()
749 if ( m_globalAccel->isEnabled() )
751 TDEAction *action = actionCollection()->action(
"new_note" );
753 action->setShortcut( m_globalAccel->shortcut(
"global_new_note" ) );
754 action = actionCollection()->action(
"new_note_clipboard" );
756 action->setShortcut( m_globalAccel->shortcut(
"global_new_note_clipboard" ) );
757 action = actionCollection()->action(
"hide_all_notes" );
759 action->setShortcut( m_globalAccel->shortcut(
"global_hide_all_notes" ) );
760 action = actionCollection()->action(
"show_all_notes" );
762 action->setShortcut( m_globalAccel->shortcut(
"global_show_all_notes" ) );
764 m_globalAccel->updateConnections();
768 TDEAction *action = actionCollection()->action(
"new_note" );
770 action->setShortcut( 0 );
771 action = actionCollection()->action(
"new_note_clipboard" );
773 action->setShortcut( 0 );
774 action = actionCollection()->action(
"hide_all_notes" );
776 action->setShortcut( 0 );
777 action = actionCollection()->action(
"show_all_notes" );
779 action->setShortcut( 0 );
783 void KNotesApp::updateNetworkListener()
787 if ( KNotesGlobalConfig::receiveNotes() )
789 m_listener->setAddress( TQString::number( KNotesGlobalConfig::port() ) );
791 m_listener->listen();
795 void KNotesApp::updateStyle()
797 KNote::setStyle( KNotesGlobalConfig::style() );
799 TQDictIterator<KNote> it( m_noteList );
800 for ( ; it.current(); ++it )
801 TQApplication::postEvent( *it,
new TQEvent( TQEvent::LayoutHint ) );
804 #include "knotesapp.moc"