summaryrefslogtreecommitdiffstats
path: root/knotes/knotesapp.cpp
blob: 4590e704b29dc8d3f426949543cd688738b97666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/*******************************************************************
 KNotes -- Notes for the KDE project

 Copyright (c) 1997-2006, The KNotes Developers

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*******************************************************************/

#include <tqclipboard.h>
#include <tqptrlist.h>
#include <tqtooltip.h>

#include <kdebug.h>
#include <tdeaction.h>
#include <kxmlguifactory.h>
#include <kxmlguibuilder.h>
#include <ksystemtray.h>
#include <tdelocale.h>
#include <kiconeffect.h>
#include <kstandarddirs.h>
#include <tdepopupmenu.h>
#include <khelpmenu.h>
#include <kfind.h>
#include <kfinddialog.h>
#include <kkeydialog.h>
#include <kglobalaccel.h>
#include <ksimpleconfig.h>
#include <twin.h>
#include <kbufferedsocket.h>
#include <kserversocket.h>

#include <libkcal/journal.h>
#include <libkcal/calendarlocal.h>

#include "knotesapp.h"
#include "knote.h"
#include "knotesalarm.h"
#include "knoteconfigdlg.h"
#include "knotesglobalconfig.h"
#include "knoteslegacy.h"
#include "knotesnetrecv.h"

#include "knotes/resourcemanager.h"

using namespace KNetwork;


class KNotesKeyDialog : public KDialogBase
{
public:
    KNotesKeyDialog( TDEGlobalAccel *globals, TQWidget *parent, const char* name = 0 )
        : KDialogBase( parent, name, true, i18n("Configure Shortcuts"), Default|Ok|Cancel, Ok )
    {
        m_keyChooser = new KKeyChooser( globals, this );
        setMainWidget( m_keyChooser );
        connect( this, TQT_SIGNAL(defaultClicked()), m_keyChooser, TQT_SLOT(allDefault()) );
    }

    void insert( TDEActionCollection *actions )
    {
        m_keyChooser->insert( actions, i18n("Note Actions") );
    }

    void configure()
    {
        if ( exec() == Accepted )
            m_keyChooser->save();
    }

private:
    KKeyChooser *m_keyChooser;
};


int KNotesApp::KNoteActionList::compareItems( TQPtrCollection::Item s1, TQPtrCollection::Item s2 )
{
    if ( ((TDEAction*)s1)->text() == ((TDEAction*)s2)->text() )
        return 0;
    return ( ((TDEAction*)s1)->text() < ((TDEAction*)s2)->text() ? -1 : 1 );
}


KNotesApp::KNotesApp()
    : DCOPObject("KNotesIface"), TQLabel( 0, 0, WType_TopLevel ),
      m_alarm( 0 ), m_listener( 0 ), m_find( 0 ), m_findPos( 0 )
{
    connect( kapp, TQT_SIGNAL(lastWindowClosed()), kapp, TQT_SLOT(quit()) );

    m_noteList.setAutoDelete( true );
    m_noteActions.setAutoDelete( true );

    // create the dock widget...
    KWin::setSystemTrayWindowFor( winId(), tqt_xrootwin() );
    TQToolTip::add( this, i18n( "KNotes: Sticky notes for TDE" ) );
    setBackgroundMode( X11ParentRelative );
    setPixmap( KSystemTray::loadIcon( "knotes" ) );

    // set the initial style
    KNote::setStyle( KNotesGlobalConfig::style() );

    // create the GUI...
    new TDEAction( i18n("New Note"), "document-new", 0,
        TQT_TQOBJECT(this), TQT_SLOT(newNote()), actionCollection(), "new_note" );
    new TDEAction( i18n("New Note From Clipboard"), "edit-paste", 0,
        TQT_TQOBJECT(this), TQT_SLOT(newNoteFromClipboard()), actionCollection(), "new_note_clipboard" );
    new TDEAction( i18n("Show All Notes"), "knotes", 0,
        TQT_TQOBJECT(this), TQT_SLOT(showAllNotes()), actionCollection(), "show_all_notes" );
    new TDEAction( i18n("Hide All Notes"), "window-close", 0,
        TQT_TQOBJECT(this), TQT_SLOT(hideAllNotes()), actionCollection(), "hide_all_notes" );
    new KHelpMenu( this, kapp->aboutData(), false, actionCollection() );

    m_findAction = KStdAction::find( TQT_TQOBJECT(this), TQT_SLOT(slotOpenFindDialog()), actionCollection() );
    KStdAction::preferences( TQT_TQOBJECT(this), TQT_SLOT(slotPreferences()), actionCollection() );
    KStdAction::keyBindings( TQT_TQOBJECT(this), TQT_SLOT(slotConfigureAccels()), actionCollection() );
    //FIXME: no shortcut removing!?
    KStdAction::quit( TQT_TQOBJECT(this), TQT_SLOT(slotQuit()), actionCollection() )->setShortcut( 0 );

    setXMLFile( instance()->instanceName() + "appui.rc" );

    m_guiBuilder = new KXMLGUIBuilder( this );
    m_guiFactory = new KXMLGUIFactory( m_guiBuilder, TQT_TQOBJECT(this) );
    m_guiFactory->addClient( this );

    m_context_menu = static_cast<TDEPopupMenu*>(m_guiFactory->container( "knotes_context", this ));
    m_note_menu = static_cast<TDEPopupMenu*>(m_guiFactory->container( "notes_menu", this ));

    // get the most recent XML UI file
    TQString xmlFileName = instance()->instanceName() + "ui.rc";
    TQString filter = TQString::fromLatin1( instance()->instanceName() + '/' ) + xmlFileName;
    TQStringList fileList = instance()->dirs()->findAllResources( "data", filter ) +
                           instance()->dirs()->findAllResources( "data", xmlFileName );

    TQString doc;
    KXMLGUIClient::findMostRecentXMLFile( fileList, doc );
    m_noteGUI.setContent( doc );

    // create accels for global shortcuts
    m_globalAccel = new TDEGlobalAccel( TQT_TQOBJECT(this), "global accel" );
    m_globalAccel->insert( "global_new_note", i18n("New Note"), "",
                           TDEShortcut(), TDEShortcut(),
                           TQT_TQOBJECT(this), TQT_SLOT(newNote()), true, true );
    m_globalAccel->insert( "global_new_note_clipboard", i18n("New Note From Clipboard"), "",
                           TDEShortcut(), TDEShortcut(),
                           TQT_TQOBJECT(this), TQT_SLOT(newNoteFromClipboard()), true, true );
    m_globalAccel->insert( "global_hide_all_notes", i18n("Hide All Notes"), "",
                           TDEShortcut(), TDEShortcut(),
                           TQT_TQOBJECT(this), TQT_SLOT(hideAllNotes()), true, true );
    m_globalAccel->insert( "global_show_all_notes", i18n("Show All Notes"), "",
                           TDEShortcut(), TDEShortcut(),
                           TQT_TQOBJECT(this), TQT_SLOT(showAllNotes()), true, true );

    m_globalAccel->readSettings();

    TDEConfig *config = TDEGlobal::config();
    config->setGroup( "Global Keybindings" );
    m_globalAccel->setEnabled( config->readBoolEntry( "Enabled", true ) );

    updateGlobalAccels();

    // clean up old config files
    KNotesLegacy::cleanUp();

    // create the resource manager
    m_manager = new KNotesResourceManager();
    connect( m_manager, TQT_SIGNAL(sigRegisteredNote( KCal::Journal * )),
             this,      TQT_SLOT(createNote( KCal::Journal * )) );
    connect( m_manager, TQT_SIGNAL(sigDeregisteredNote( KCal::Journal * )),
             this,      TQT_SLOT(killNote( KCal::Journal * )) );

    // read the notes
    m_manager->load();

    // read the old config files, convert and add them
    KCal::CalendarLocal calendar( TQString::fromLatin1( "UTC" ) );
    if ( KNotesLegacy::convert( &calendar ) )
    {
        KCal::Journal::List notes = calendar.journals();
        KCal::Journal::List::ConstIterator it;
        for ( it = notes.constBegin(); it != notes.constEnd(); ++it )
            m_manager->addNewNote( *it );

        m_manager->save();
    }

    // set up the alarm reminder - do it after loading the notes because this
    // is used as a check if updateNoteActions has to be called for a new note
    m_alarm = new KNotesAlarm( m_manager, TQT_TQOBJECT(this) );

    // create the socket and possibly start listening for connections
    m_listener = new TDEServerSocket();
    m_listener->setResolutionEnabled( true );
    connect( m_listener, TQT_SIGNAL(readyAccept()), TQT_SLOT(acceptConnection()) );
    updateNetworkListener();

    if ( m_noteList.count() == 0 && !kapp->isRestored() )
        newNote();

    updateNoteActions();
}

void KNotesApp::resizeTrayIcon ()
{
	// Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
	TQPixmap origpixmap;
	TQPixmap scaledpixmap;
	TQImage newIcon;
	origpixmap = KSystemTray::loadSizedIcon( "knotes", TQWidget::width() );
	newIcon = origpixmap;
	newIcon = newIcon.smoothScale(TQWidget::width(), TQWidget::height());
	scaledpixmap = newIcon;
	setPixmap(scaledpixmap);
}

void KNotesApp::resizeEvent ( TQResizeEvent * )
{
	// Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
	resizeTrayIcon();
}

void KNotesApp::showEvent ( TQShowEvent * )
{
	// Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
	resizeTrayIcon();
}

KNotesApp::~KNotesApp()
{
    saveNotes();

    blockSignals( true );
    m_noteList.clear();
    blockSignals( false );

    delete m_listener;
    delete m_manager;
    delete m_guiBuilder;
}

bool KNotesApp::commitData( TQSessionManager& )
{
    saveConfigs();
    return true;
}

// -------------------- public DCOP interface -------------------- //

TQString KNotesApp::newNote( const TQString& name, const TQString& text )
{
    // create the new note
    KCal::Journal *journal = new KCal::Journal();

    // new notes have the current date/time as title if none was given
    if ( !name.isEmpty() )
        journal->setSummary( name );
    else
        journal->setSummary( TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime() ) );

    // the body of the note
    journal->setDescription( text );

    if ( m_manager->addNewNote( journal ) ) {
        showNote( journal->uid() );
    }
    return journal->uid();
}

TQString KNotesApp::newNoteFromClipboard( const TQString& name )
{
    const TQString& text = TDEApplication::clipboard()->text();
    return newNote( name, text );
}

void KNotesApp::hideAllNotes() const
{
    TQDictIterator<KNote> it( m_noteList );
    for ( ; *it; ++it )
        (*it)->close();
}

void KNotesApp::showAllNotes() const
{
    TQDictIterator<KNote> it( m_noteList );
    for ( ; *it; ++it )
    {
        (*it)->show();
    }
}

void KNotesApp::showNote( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        showNote( note );
    else
        kdWarning(5500) << "showNote: no note with id: " << id << endl;
}

void KNotesApp::hideNote( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        note->hide();
    else
        kdWarning(5500) << "hideNote: no note with id: " << id << endl;
}

void KNotesApp::killNote( const TQString& id, bool force )
{
    KNote* note = m_noteList[id];
    if ( note )
        note->slotKill( force );
    else
        kdWarning(5500) << "killNote: no note with id: " << id << endl;
}

// "bool force = false" doesn't work with dcop
void KNotesApp::killNote( const TQString& id )
{
    killNote( id, false );
}

TQMap<TQString,TQString> KNotesApp::notes() const
{
    TQMap<TQString,TQString> notes;
    TQDictIterator<KNote> it( m_noteList );

    for ( ; it.current(); ++it )
        notes.insert( it.current()->noteId(), it.current()->name() );

    return notes;
}

TQDateTime KNotesApp::getLastModified( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    TQDateTime d;
    if ( note )
        d = note->getLastModified();
    return d;
}

TQString KNotesApp::name( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->name();
    else
        return TQString();
}

TQString KNotesApp::text( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->text();
    else
        return TQString();
}

void KNotesApp::setName( const TQString& id, const TQString& newName )
{
    KNote* note = m_noteList[id];
    if ( note )
        note->setName( newName );
    else
        kdWarning(5500) << "setName: no note with id: " << id << endl;
}

void KNotesApp::setText( const TQString& id, const TQString& newText )
{
    KNote* note = m_noteList[id];
    if ( note )
        note->setText( newText );
    else
        kdWarning(5500) << "setText: no note with id: " << id << endl;
}

TQString KNotesApp::fgColor( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->fgColor().name();
    else
        return TQString();
}

TQString KNotesApp::bgColor( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->bgColor().name();
    else
        return TQString();
}

void KNotesApp::setColor( const TQString& id, const TQString& fgColor, const TQString& bgColor )
{
    KNote* note = m_noteList[id];
    if ( note )
        note->setColor( TQColor( fgColor ), TQColor( bgColor ) );
    else
        kdWarning(5500) << "setColor: no note with id: " << id << endl;
}

int KNotesApp::width( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->width();
    else
        return 0;
}

int KNotesApp::height( const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->height();
    else
        return 0;
}

void KNotesApp::move( const TQString& id, int x, int y ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->move( x, y );
    else
        kdWarning(5500) << "move: no note with id: " << id << endl;
}

void KNotesApp::resize( const TQString& id, int width, int height ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->resize( width, height );
    else
        kdWarning(5500) << "resize: no note with id: " << id << endl;
}

void KNotesApp::sync( const TQString& app )
{
    TQDictIterator<KNote> it( m_noteList );

    for ( ; it.current(); ++it )
        it.current()->sync( app );
}

bool KNotesApp::isNew( const TQString& app, const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->isNew( app );
    else
        return false;
}

bool KNotesApp::isModified( const TQString& app, const TQString& id ) const
{
    KNote* note = m_noteList[id];
    if ( note )
        return note->isModified( app );
    else
        return false;
}


// ------------------- protected methods ------------------- //

void KNotesApp::mousePressEvent( TQMouseEvent* e )
{
    if ( !rect().contains( e->pos() ) )
        return;

    switch ( e->button() )
    {
    case Qt::LeftButton:
        if ( m_noteList.count() == 1 )
        {
            TQDictIterator<KNote> it( m_noteList );
            showNote( it.toFirst() );
        }
        else if ( m_note_menu->count() > 0 )
            m_note_menu->popup( e->globalPos() );
        break;
    case Qt::MidButton:
        newNote();
        break;
    case Qt::RightButton:
        m_context_menu->popup( e->globalPos() );
    default: break;
    }
}

// -------------------- protected slots -------------------- //

void KNotesApp::slotShowNote()
{
    // tell the WM to give this note focus
    showNote( TQString::fromUtf8( TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name() ) );
}

void KNotesApp::slotWalkThroughNotes()
{
    // show next note
    TQDictIterator<KNote> it( m_noteList );
    KNote *first = it.toFirst();
    for ( ; *it; ++it )
        if ( (*it)->hasFocus() )
        {
            if ( ++it )
                showNote( *it );
            else
                showNote( first );
            break;
        }
}

void KNotesApp::slotOpenFindDialog()
{
    KFindDialog findDia( this, "find_dialog" );
    findDia.setHasSelection( false );
    findDia.setHasCursor( false );
    findDia.setSupportsBackwardsFind( false );

    if ( (findDia.exec() != TQDialog::Accepted) || findDia.pattern().isEmpty() )
        return;

    delete m_findPos;
    m_findPos = new TQDictIterator<KNote>( m_noteList );

    // this could be in an own method if searching without a dialog should be possible
    delete m_find;
    m_find = new KFind( findDia.pattern(), findDia.options(), this );

    slotFindNext();
}

void KNotesApp::slotFindNext()
{
    if ( **m_findPos )
    {
        KNote *note = **m_findPos;
        ++*m_findPos;
        note->find( m_find->pattern(), m_find->options() );
    }
    else
    {
        m_find->displayFinalDialog();
        delete m_find;
        m_find = 0;
        delete m_findPos;
        m_findPos = 0;
    }
}

void KNotesApp::slotPreferences()
{
    // reuse the dialog if possible
    if ( KNoteConfigDlg::showDialog( "KNotes Default Settings" ) )
        return;

    // create a new preferences dialog...
    KNoteConfigDlg *dialog = new KNoteConfigDlg( 0, i18n("Settings"), this,
                                                 "KNotes Settings" );
    connect( dialog, TQT_SIGNAL(settingsChanged()), TQT_TQOBJECT(this), TQT_SLOT(updateNetworkListener()) );
    connect( dialog, TQT_SIGNAL(settingsChanged()), TQT_TQOBJECT(this), TQT_SLOT(updateStyle()) );
    dialog->show();
}

void KNotesApp::slotConfigureAccels()
{
    KNotesKeyDialog keys( m_globalAccel, this );
    TQDictIterator<KNote> notes( m_noteList );
    if ( !m_noteList.isEmpty() )
        keys.insert( (*notes)->actionCollection() );
    keys.configure();

    m_globalAccel->writeSettings();
    updateGlobalAccels();

    // update GUI doc for new notes
    m_noteGUI.setContent(
        KXMLGUIFactory::readConfigFile( instance()->instanceName() + "ui.rc", instance() )
    );

    if ( m_noteList.isEmpty() )
        return;

    notes.toFirst();
    TQValueList<TDEAction *> list = (*notes)->actionCollection()->actions();
    for ( TQValueList<TDEAction *>::iterator it = list.begin(); it != list.end(); ++it )
    {
        notes.toFirst();
        for ( ++notes; *notes; ++notes )
        {
            TDEAction *toChange = (*notes)->actionCollection()->action( (*it)->name() );
            if ( toChange->shortcut() != (*it)->shortcut() )
                toChange->setShortcut( (*it)->shortcut() );
        }
    }
}

void KNotesApp::slotNoteKilled( KCal::Journal *journal )
{
    m_noteUidModify="";
    m_manager->deleteNote( journal );
    saveNotes();
}

void KNotesApp::slotQuit()
{
    TQDictIterator<KNote> it( m_noteList );

    for ( ; *it; ++it )
        if ( (*it)->isModified() )
            (*it)->saveData(false);

    saveConfigs();
    kapp->quit();
}


// -------------------- private methods -------------------- //

void KNotesApp::showNote( KNote* note ) const
{
    note->show();
    KWin::setCurrentDesktop( KWin::windowInfo( note->winId() ).desktop() );
    KWin::forceActiveWindow( note->winId() );
    note->setFocus();
}

void KNotesApp::createNote( KCal::Journal *journal )
{
  if( journal->uid() == m_noteUidModify)
  {
         KNote *note = m_noteList[m_noteUidModify];
         if ( note )
                 note->changeJournal(journal);

         return;
  }
  m_noteUidModify = journal->uid();
    KNote *newNote = new KNote( m_noteGUI, journal, 0, journal->uid().utf8() );
    m_noteList.insert( newNote->noteId(), newNote );

    connect( newNote, TQT_SIGNAL(sigRequestNewNote()), TQT_SLOT(newNote()) );
    connect( newNote, TQT_SIGNAL(sigShowNextNote()), TQT_SLOT(slotWalkThroughNotes()) );
    connect( newNote, TQT_SIGNAL(sigKillNote( KCal::Journal* )),
                        TQT_SLOT(slotNoteKilled( KCal::Journal* )) );
    connect( newNote, TQT_SIGNAL(sigNameChanged()), TQT_SLOT(updateNoteActions()) );
    connect( newNote, TQT_SIGNAL(sigDataChanged(const TQString &)), TQT_SLOT(saveNotes(const TQString &)) );
    connect( newNote, TQT_SIGNAL(sigColorChanged()), TQT_SLOT(updateNoteActions()) );
    connect( newNote, TQT_SIGNAL(sigFindFinished()), TQT_SLOT(slotFindNext()) );

    // don't call this during startup for each and every loaded note
    if ( m_alarm )
        updateNoteActions();
}

void KNotesApp::killNote( KCal::Journal *journal )
{
  if(m_noteUidModify == journal->uid())
  {
         return;
  }
    // this kills the KNote object
    KNote *note = m_noteList.take( journal->uid() );
    if ( note )
    {
        note->deleteWhenIdle();
        updateNoteActions();
    }
}

void KNotesApp::acceptConnection()
{
    // Accept the connection and make KNotesNetworkReceiver do the job
    TDEBufferedSocket *s = static_cast<TDEBufferedSocket *>(m_listener->accept());
    if ( s )
    {
        KNotesNetworkReceiver *recv = new KNotesNetworkReceiver( s );
        connect( recv, TQT_SIGNAL(sigNoteReceived( const TQString &, const TQString & )),
                 TQT_TQOBJECT(this), TQT_SLOT(newNote( const TQString &, const TQString & )) );
    }
}

void KNotesApp::saveNotes( const TQString & uid )
{
  m_noteUidModify = uid;
  saveNotes();
}

void KNotesApp::saveNotes()
{
    KNotesGlobalConfig::writeConfig();
    m_manager->save();
}

void KNotesApp::saveConfigs()
{
    TQDictIterator<KNote> it( m_noteList );
    for ( ; it.current(); ++it )
        it.current()->saveConfig();
}

void KNotesApp::updateNoteActions()
{
    unplugActionList( "notes" );
    m_noteActions.clear();

    for ( TQDictIterator<KNote> it( m_noteList ); it.current(); ++it )
    {
        TDEAction *action = new TDEAction( it.current()->name().replace("&", "&&"),
                                       TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT(slotShowNote()),
                                       (TQObject *)0,
                                       it.current()->noteId().utf8() );
        TDEIconEffect effect;
        TQPixmap icon = effect.apply( kapp->miniIcon(), TDEIconEffect::Colorize, 1,
                                     it.current()->paletteBackgroundColor(), false );
        action->setIconSet( icon );
        m_noteActions.append( action );
    }

    if ( m_noteActions.isEmpty() )
    {
        actionCollection()->action( "hide_all_notes" )->setEnabled( false );
        actionCollection()->action( "show_all_notes" )->setEnabled( false );
        m_findAction->setEnabled( false );
        TDEAction *action = new TDEAction( i18n("No Notes") );
        m_noteActions.append( action );
    }
    else
    {
        actionCollection()->action( "hide_all_notes" )->setEnabled( true );
        actionCollection()->action( "show_all_notes" )->setEnabled( true );
        m_findAction->setEnabled( true );
        m_noteActions.sort();
    }
    plugActionList( "notes", m_noteActions );
}

void KNotesApp::updateGlobalAccels()
{
    if ( m_globalAccel->isEnabled() )
    {
        TDEAction *action = actionCollection()->action( "new_note" );
        if ( action )
            action->setShortcut( m_globalAccel->shortcut( "global_new_note" ) );
        action = actionCollection()->action( "new_note_clipboard" );
        if ( action )
            action->setShortcut( m_globalAccel->shortcut( "global_new_note_clipboard" ) );
        action = actionCollection()->action( "hide_all_notes" );
        if ( action )
            action->setShortcut( m_globalAccel->shortcut( "global_hide_all_notes" ) );
        action = actionCollection()->action( "show_all_notes" );
        if ( action )
            action->setShortcut( m_globalAccel->shortcut( "global_show_all_notes" ) );

        m_globalAccel->updateConnections();
    }
    else
    {
        TDEAction *action = actionCollection()->action( "new_note" );
        if ( action )
            action->setShortcut( 0 );
        action = actionCollection()->action( "new_note_clipboard" );
        if ( action )
            action->setShortcut( 0 );
        action = actionCollection()->action( "hide_all_notes" );
        if ( action )
            action->setShortcut( 0 );
        action = actionCollection()->action( "show_all_notes" );
        if ( action )
            action->setShortcut( 0 );
    }
}

void KNotesApp::updateNetworkListener()
{
    m_listener->close();

    if ( KNotesGlobalConfig::receiveNotes() )
    {
        m_listener->setAddress( TQString::number( KNotesGlobalConfig::port() ) );
        m_listener->bind();
        m_listener->listen();
    }
}

void KNotesApp::updateStyle()
{
    KNote::setStyle( KNotesGlobalConfig::style() );

    TQDictIterator<KNote> it( m_noteList );
    for ( ; it.current(); ++it )
        TQApplication::postEvent( *it, new TQEvent( TQEvent::LayoutHint ) );
}

#include "knotesapp.moc"