24 #include "knotesadaptor.h"
29 #include "knotesglobalconfig.h"
37 #include <kactioncollection.h>
41 #include <kfinddialog.h>
42 #include <khelpmenu.h>
44 #include <kiconeffect.h>
47 #include <kshortcutsdialog.h>
48 #include <ksocketfactory.h>
49 #include <kstandardaction.h>
50 #include <kstandarddirs.h>
51 #include <ksystemtrayicon.h>
52 #include <kwindowsystem.h>
53 #include <kxmlguibuilder.h>
54 #include <kxmlguifactory.h>
56 #include <kcal/calendarlocal.h>
57 #include <kcal/journal.h>
58 #include <kiconloader.h>
64 #include <dnssd/publicservice.h>
68 return ( a1->text() < a2->text() );
80 new KNotesAdaptor(
this );
81 QDBusConnection::sessionBus().registerObject( QLatin1String(
"/KNotes") ,
this );
82 kapp->setQuitOnLastWindowClosed(
false );
87 connect( m_tray, SIGNAL(activateRequested(
bool,QPoint)),
this, SLOT(
slotActivateRequested(
bool,QPoint)) );
91 KAction *action =
new KAction( KIcon( QLatin1String(
"document-new") ),
92 i18n(
"New Note" ),
this );
93 actionCollection()->addAction( QLatin1String(
"new_note"), action );
94 action->setGlobalShortcut( KShortcut( Qt::ALT + Qt::SHIFT + Qt::Key_N ));
95 connect( action, SIGNAL(triggered()), SLOT(
newNote()) );
97 action =
new KAction( KIcon( QLatin1String(
"edit-paste") ),
98 i18n(
"New Note From Clipboard" ),
this );
99 actionCollection()->addAction( QLatin1String(
"new_note_clipboard"), action );
100 action->setGlobalShortcut( KShortcut( Qt::ALT + Qt::SHIFT + Qt::Key_C ));
103 action =
new KAction( KIcon(QLatin1String(
"knotes") ), i18n(
"Show All Notes" ),
this );
104 actionCollection()->addAction( QLatin1String(
"show_all_notes"), action );
105 action->setGlobalShortcut( KShortcut( Qt::ALT + Qt::SHIFT + Qt::Key_S ));
106 connect( action, SIGNAL(triggered()), SLOT(
showAllNotes()) );
108 action =
new KAction( KIcon( QLatin1String(
"window-close") ),
109 i18n(
"Hide All Notes" ),
this );
110 actionCollection()->addAction( QLatin1String(
"hide_all_notes"), action );
111 action->setGlobalShortcut( KShortcut( Qt::ALT + Qt::SHIFT + Qt::Key_H ));
112 connect( action, SIGNAL(triggered()), SLOT(
hideAllNotes()) );
114 action =
new KAction( KIcon( QLatin1String(
"document-print") ),
115 i18nc(
"@action:inmenu",
"Print Selected Notes..." ),
this );
116 actionCollection()->addAction( QLatin1String(
"print_selected_notes"), action );
117 connect( action, SIGNAL(triggered()), SLOT(slotPrintSelectedNotes()) );
120 new KHelpMenu(
this, KGlobal::mainComponent().aboutData(),
false,
121 actionCollection() );
124 actionCollection() );
126 actionCollection() );
128 actionCollection() );
130 KStandardAction::quit(
this, SLOT(
slotQuit()),
131 actionCollection() )->setShortcut( 0 );
133 setXMLFile( componentData().componentName() + QLatin1String(
"appui.rc") );
135 m_guiBuilder =
new KXMLGUIBuilder(
this );
136 m_guiFactory =
new KXMLGUIFactory( m_guiBuilder,
this );
137 m_guiFactory->addClient(
this );
139 m_contextMenu =
static_cast<KMenu *
>( m_guiFactory->container(
140 QLatin1String(
"knotes_context"),
142 m_noteMenu =
static_cast<KMenu *
>( m_guiFactory->container(
143 QLatin1String(
"notes_menu"),
this ) );
144 m_tray->setContextMenu( m_contextMenu );
146 QString xmlFileName = componentData().componentName() + QLatin1String(
"ui.rc");
147 QString filter = componentData().componentName() + QLatin1Char(
'/') + xmlFileName;
148 const QStringList fileList =
149 componentData().dirs()->findAllResources(
"data", filter ) +
150 componentData().dirs()->findAllResources(
"data", xmlFileName );
153 KXMLGUIClient::findMostRecentXMLFile( fileList, doc );
154 m_noteGUI.setContent( doc );
155 const bool needConvert = (KNotesGlobalConfig::self()->notesVersion()<1);
163 connect( m_manager, SIGNAL(sigRegisteredNote(KCal::Journal*)),
164 this, SLOT(createNote(KCal::Journal*)) );
165 connect( m_manager, SIGNAL(sigDeregisteredNote(KCal::Journal*)),
166 this, SLOT(
killNote(KCal::Journal*)) );
173 KCal::CalendarLocal calendar( QString::fromLatin1(
"UTC" ) );
175 KCal::Journal::List
notes = calendar.journals();
176 KCal::Journal::List::ConstIterator it;
177 KCal::Journal::List::ConstIterator end(notes.constEnd());
178 for ( it = notes.constBegin(); it != end; ++it ) {
184 KNotesGlobalConfig::self()->setNotesVersion(1);
192 updateNetworkListener();
208 blockSignals(
true );
209 qDeleteAll( m_notes );
211 qDeleteAll( m_noteActions );
212 m_noteActions.clear();
213 blockSignals(
false );
228 foreach (
KNote *note, m_notes ) {
240 KCal::Journal *journal =
new KCal::Journal();
243 if ( !name.isEmpty() ) {
244 journal->setSummary( name );
246 journal->setSummary( KGlobal::locale()->formatDateTime(
247 QDateTime::currentDateTime() ) );
251 journal->setDescription( text );
257 return journal->uid();
262 const QString &
text = KApplication::clipboard()->text();
268 foreach (
KNote *note, m_notes ) {
275 foreach (
KNote *note, m_notes ) {
285 KNote *note = m_notes.value(
id );
289 kWarning( 5500 ) <<
"showNote: no note with id:" << id;
295 KNote *note = m_notes.value(
id );
299 kWarning( 5500 ) <<
"hideNote: no note with id:" << id;
305 KNote *note = m_notes.value(
id );
309 kWarning( 5500 ) <<
"killNote: no note with id:" << id;
323 foreach (
KNote *note, m_notes ) {
332 KNote *note = m_notes.value(
id );
342 KNote *note = m_notes.value(
id );
352 KNote *note = m_notes.value(
id );
356 kWarning( 5500 ) <<
"setName: no note with id:" << id;
362 KNote *note = m_notes.value(
id );
366 kWarning( 5500 ) <<
"setText: no note with id:" << id;
374 if ( m_notes.size() == 1 ) {
377 m_noteMenu->popup( QCursor::pos ());
395 QMap<QString, KNote *>::const_iterator it = m_notes.constBegin();
396 QMap<QString, KNote *>::const_iterator end(m_notes.constEnd());
397 for ( ; it != end; ++it ) {
398 if ( ( *it )->hasFocus() ) {
411 KFindDialog findDia(
this );
412 findDia.setObjectName( QLatin1String(
"find_dialog") );
413 findDia.setHasSelection(
false );
414 findDia.setHasCursor(
false );
415 findDia.setSupportsBackwardsFind(
false );
417 if ( (findDia.exec() != QDialog::Accepted) ) {
426 m_findPos =
new QMap<QString, KNote *>::iterator();
427 *m_findPos = m_notes.begin();
432 m_find =
new KFind( findDia.pattern(), findDia.options(), this );
439 if ( *m_findPos != m_notes.end() ) {
440 KNote *note = * ( (*m_findPos)++ );
441 note->
find( m_find );
443 m_find->displayFinalDialog();
444 m_find->deleteLater();
455 connect( dialog, SIGNAL(configWrote()),
this, SLOT(slotConfigUpdated()));
459 void KNotesApp::slotConfigUpdated()
461 updateNetworkListener();
466 QPointer<KNotesKeyDialog> keys =
new KNotesKeyDialog( actionCollection(),
this );
468 QMap<QString, KNote *>::const_iterator it = m_notes.constBegin();
470 if ( !m_notes.isEmpty() ) {
471 keys->insert( ( *it )->actionCollection() );
478 m_noteGUI.setContent(
479 KXMLGUIFactory::readConfigFile( componentData().componentName() + QLatin1String(
"ui.rc"),
483 if ( !m_notes.isEmpty() ) {
484 QMap<QString, KNote *>::const_iterator end = m_notes.constEnd();
486 foreach ( QAction *action, ( *it )->actionCollection()->actions() ) {
487 it = m_notes.constBegin();
489 for ( ++it; it != end; ++it ) {
499 QAction *toChange = ( *it )->actionCollection()->action( action->objectName() );
501 toChange->setShortcuts( action->shortcuts() );
512 m_noteUidModify.clear();
520 foreach (
KNote *note, m_notes ) {
536 note->
toDesktop( KWindowSystem::currentDesktop() );
538 KWindowSystem::setCurrentDesktop(
539 KWindowSystem::windowInfo( note->winId(), NET::WMDesktop ).desktop() );
541 KWindowSystem::forceActiveWindow( note->winId() );
546 void KNotesApp::createNote( KCal::Journal *journal )
548 if( journal->uid() == m_noteUidModify)
550 KNote *note = m_notes.value( m_noteUidModify );
556 m_noteUidModify = journal->uid();
560 connect( newNote, SIGNAL(sigRequestNewNote()),
562 connect( newNote, SIGNAL(sigShowNextNote()),
564 connect( newNote, SIGNAL(sigKillNote(KCal::Journal*)),
566 connect( newNote, SIGNAL(sigNameChanged(QString)),
567 SLOT(updateNoteActions()) );
568 connect( newNote, SIGNAL(sigDataChanged(QString)),
569 SLOT(saveNotes(QString)) );
570 connect( newNote, SIGNAL(sigColorChanged()),
571 SLOT(updateNoteActions()) );
572 connect( newNote, SIGNAL(sigFindFinished()),
582 if (m_tray->isVisible()) {
592 if(m_noteUidModify == journal->uid()) {
596 KNote *note = m_notes.take( journal->uid() );
603 void KNotesApp::acceptConnection()
606 QTcpSocket *s = m_listener->nextPendingConnection();
611 SIGNAL(sigNoteReceived(QString,QString)),
612 SLOT(
newNote(QString,QString)) );
616 void KNotesApp::saveNotes(
const QString & uid )
618 m_noteUidModify = uid;
622 void KNotesApp::saveNotes()
624 KNotesGlobalConfig::self()->writeConfig();
628 void KNotesApp::saveConfigs()
630 foreach (
KNote *note, m_notes ) {
635 void KNotesApp::updateNoteActions()
637 unplugActionList( QLatin1String(
"notes") );
638 m_noteActions.clear();
640 foreach (
KNote *note, m_notes ) {
643 #warning utf8: use QString
645 KAction *action =
new KAction( note->
name().replace( QLatin1String(
"&"), QLatin1String(
"&&") ), this );
646 action->setObjectName( note->
noteId() );
647 connect( action, SIGNAL(triggered(
bool)), SLOT(
slotShowNote()) );
650 effect.apply( qApp->windowIcon().pixmap( IconSize( KIconLoader::Small ),
651 IconSize( KIconLoader::Small ) ),
652 KIconEffect::Colorize,
654 note->palette().color( note->backgroundRole() ),
657 action->setIcon( icon );
658 m_noteActions.append( action );
661 if ( m_noteActions.isEmpty() ) {
662 actionCollection()->action( QLatin1String(
"hide_all_notes") )->setEnabled(
false );
663 actionCollection()->action( QLatin1String(
"show_all_notes") )->setEnabled(
false );
664 actionCollection()->action( QLatin1String(
"print_selected_notes") )->setEnabled(
false );
665 m_findAction->setEnabled(
false );
666 KAction *action =
new KAction( i18n(
"No Notes" ),
this );
667 m_noteActions.append( action );
670 actionCollection()->action( QLatin1String(
"hide_all_notes") )->setEnabled(
true );
671 actionCollection()->action( QLatin1String(
"show_all_notes") )->setEnabled(
true );
672 actionCollection()->action( QLatin1String(
"print_selected_notes") )->setEnabled(
true );
673 m_findAction->setEnabled(
true );
675 plugActionList( QLatin1String(
"notes"), m_noteActions );
678 void KNotesApp::updateNetworkListener()
685 if ( KNotesGlobalConfig::receiveNotes() ) {
687 m_listener=KSocketFactory::listen( QLatin1String(
"knotes") , QHostAddress::Any,
688 KNotesGlobalConfig::port() );
689 connect( m_listener, SIGNAL(newConnection()),
690 SLOT(acceptConnection()) );
691 m_publisher=
new DNSSD::PublicService(KNotesGlobalConfig::senderID(), QLatin1String(
"_knotes._tcp"), KNotesGlobalConfig::port());
692 m_publisher->publishAsync();
696 void KNotesApp::slotPrintSelectedNotes()
699 dlg->setNotes(m_notes);
701 const QList<KNotePrintObject *> lst = dlg->selectedNotes();
702 if (!lst.isEmpty()) {
703 const QString selectedTheme = dlg->selectedTheme();
705 printer.
printNotes( lst, selectedTheme, dlg->preview() );
712 #include "knotesapp.moc"
void slotKill(bool force=false)
void showAllNotes() const
void slotNoteKilled(KCal::Journal *journal)
QVariantMap notes() const
void printNotes(const QList< KNotePrintObject * > lst, const QString &themePath, bool preview)
QString text(const QString &id) const
void changeJournal(KCal::Journal *)
bool isDesktopAssigned() const
void slotSecondaryActivateRequested(const QPoint &)
void deleteNote(KCal::Journal *journal)
void hideNote(const QString &id) const
QString newNote(const QString &name=QString(), const QString &text=QString())
void hideAllNotes() const
void setText(const QString &id, const QString &newText)
QString newNoteFromClipboard(const QString &name=QString())
void killNote(const QString &id)
static bool convert(KCal::CalendarLocal *calendar)
void slotActivateRequested(bool, const QPoint &pos)
void slotConfigureAccels()
void showNote(const QString &id) const
void saveData(bool update=true)
QString name(const QString &id) const
void setName(const QString &id, const QString &newName)
bool commitData(QSessionManager &)
void setName(const QString &name)
bool addNewNote(KCal::Journal *journal)
void slotWalkThroughNotes()
void slotOpenFindDialog()
void toDesktop(int desktop)
static bool qActionLessThan(const QAction *a1, const QAction *a2)
void updateNumberOfNotes(int value)
void setText(const QString &text)