21#include <tqclipboard.h> 
   27#include <kxmlguifactory.h> 
   28#include <kxmlguibuilder.h> 
   29#include <ksystemtray.h> 
   31#include <kiconeffect.h> 
   32#include <tdestandarddirs.h> 
   33#include <tdepopupmenu.h> 
   36#include <kfinddialog.h> 
   37#include <kkeydialog.h> 
   38#include <tdeglobalaccel.h> 
   39#include <tdesimpleconfig.h> 
   41#include <kbufferedsocket.h> 
   42#include <kserversocket.h> 
   44#include <libkcal/journal.h> 
   45#include <libkcal/calendarlocal.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" 
   57using namespace KNetwork;
 
   60class 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, TQ_SIGNAL(defaultClicked()), m_keyChooser, TQ_SLOT(allDefault()) );
 
   71    void insert( TDEActionCollection *actions )
 
   73        m_keyChooser->insert( actions, i18n(
"Note Actions") );
 
   78        if ( exec() == Accepted )
 
   83    KKeyChooser *m_keyChooser;
 
   87int 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 );
 
   96    : DCOPObject(
"KNotesIface"), TQLabel( 0, 0, WType_TopLevel ),
 
   97      m_alarm( 0 ), m_listener( 0 ), m_find( 0 ), m_findPos( 0 )
 
   99    connect( tdeApp, TQ_SIGNAL(lastWindowClosed()), tdeApp, TQ_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        this, TQ_SLOT(newNote()), actionCollection(), 
"new_note" );
 
  116    new TDEAction( i18n(
"New Note From Clipboard"), 
"edit-paste", 0,
 
  117        this, TQ_SLOT(newNoteFromClipboard()), actionCollection(), 
"new_note_clipboard" );
 
  118    new TDEAction( i18n(
"Show All Notes"), 
"knotes", 0,
 
  119        this, TQ_SLOT(showAllNotes()), actionCollection(), 
"show_all_notes" );
 
  120    new TDEAction( i18n(
"Hide All Notes"), 
"window-close", 0,
 
  121        this, TQ_SLOT(hideAllNotes()), actionCollection(), 
"hide_all_notes" );
 
  122    new KHelpMenu( 
this, tdeApp->aboutData(), 
false, actionCollection() );
 
  124    m_findAction = KStdAction::find( 
this, TQ_SLOT(slotOpenFindDialog()), actionCollection() );
 
  125    KStdAction::preferences( 
this, TQ_SLOT(slotPreferences()), actionCollection() );
 
  126    KStdAction::keyBindings( 
this, TQ_SLOT(slotConfigureAccels()), actionCollection() );
 
  128    KStdAction::quit( 
this, TQ_SLOT(slotQuit()), actionCollection() )->setShortcut( 0 );
 
  130    setXMLFile( instance()->instanceName() + 
"appui.rc" );
 
  132    m_guiBuilder = 
new KXMLGUIBuilder( 
this );
 
  133    m_guiFactory = 
new KXMLGUIFactory( m_guiBuilder, 
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( 
this, 
"global accel" );
 
  151    m_globalAccel->insert( 
"global_new_note", i18n(
"New Note"), 
"",
 
  152                           TDEShortcut(), TDEShortcut(),
 
  153                           this, TQ_SLOT(newNote()), 
true, 
true );
 
  154    m_globalAccel->insert( 
"global_new_note_clipboard", i18n(
"New Note From Clipboard"), 
"",
 
  155                           TDEShortcut(), TDEShortcut(),
 
  156                           this, TQ_SLOT(newNoteFromClipboard()), 
true, 
true );
 
  157    m_globalAccel->insert( 
"global_hide_all_notes", i18n(
"Hide All Notes"), 
"",
 
  158                           TDEShortcut(), TDEShortcut(),
 
  159                           this, TQ_SLOT(hideAllNotes()), 
true, 
true );
 
  160    m_globalAccel->insert( 
"global_show_all_notes", i18n(
"Show All Notes"), 
"",
 
  161                           TDEShortcut(), TDEShortcut(),
 
  162                           this, TQ_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, TQ_SIGNAL(sigRegisteredNote( KCal::Journal * )),
 
  178             this,      TQ_SLOT(createNote( KCal::Journal * )) );
 
  179    connect( m_manager, TQ_SIGNAL(sigDeregisteredNote( KCal::Journal * )),
 
  180             this,      TQ_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, 
this );
 
  202    m_listener = 
new TDEServerSocket();
 
  203    m_listener->setResolutionEnabled( 
true );
 
  204    connect( m_listener, TQ_SIGNAL(readyAccept()), TQ_SLOT(acceptConnection()) );
 
  205    updateNetworkListener();
 
  207    if ( m_noteList.count() == 0 && !tdeApp->isRestored() )
 
  213void 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);
 
  226void KNotesApp::resizeEvent ( TQResizeEvent * )
 
  232void KNotesApp::showEvent ( TQShowEvent * )
 
  238KNotesApp::~KNotesApp()
 
  242    blockSignals( 
true );
 
  244    blockSignals( 
false );
 
  251bool KNotesApp::commitData( TQSessionManager& )
 
  259TQString 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();
 
  279TQString KNotesApp::newNoteFromClipboard( 
const TQString& name )
 
  281    const TQString& text = TDEApplication::clipboard()->text();
 
  282    return newNote( name, text );
 
  285void KNotesApp::hideAllNotes()
 const 
  287    TQDictIterator<KNote> it( m_noteList );
 
  292void KNotesApp::showAllNotes()
 const 
  294    TQDictIterator<KNote> it( m_noteList );
 
  301void KNotesApp::showNote( 
const TQString& 
id )
 const 
  303    KNote* note = m_noteList[id];
 
  307        kdWarning(5500) << 
"showNote: no note with id: " << 
id << endl;
 
  310void KNotesApp::hideNote( 
const TQString& 
id )
 const 
  312    KNote* note = m_noteList[id];
 
  316        kdWarning(5500) << 
"hideNote: no note with id: " << 
id << endl;
 
  319void 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;
 
  329void KNotesApp::killNote( 
const TQString& 
id )
 
  331    killNote( 
id, 
false );
 
  334TQMap<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() );
 
  345TQDateTime KNotesApp::getLastModified( 
const TQString& 
id )
 const 
  347    KNote* note = m_noteList[id];
 
  350        d = note->getLastModified();
 
  354TQString KNotesApp::name( 
const TQString& 
id )
 const 
  356    KNote* note = m_noteList[id];
 
  363TQString KNotesApp::text( 
const TQString& 
id )
 const 
  365    KNote* note = m_noteList[id];
 
  372void KNotesApp::setName( 
const TQString& 
id, 
const TQString& newName )
 
  374    KNote* note = m_noteList[id];
 
  376        note->setName( newName );
 
  378        kdWarning(5500) << 
"setName: no note with id: " << 
id << endl;
 
  381void KNotesApp::setText( 
const TQString& 
id, 
const TQString& newText )
 
  383    KNote* note = m_noteList[id];
 
  385        note->setText( newText );
 
  387        kdWarning(5500) << 
"setText: no note with id: " << 
id << endl;
 
  390TQString KNotesApp::fgColor( 
const TQString& 
id )
 const 
  392    KNote* note = m_noteList[id];
 
  394        return note->fgColor().name();
 
  399TQString KNotesApp::bgColor( 
const TQString& 
id )
 const 
  401    KNote* note = m_noteList[id];
 
  403        return note->bgColor().name();
 
  408void KNotesApp::setColor( 
const TQString& 
id, 
const TQString& fgColor, 
const TQString& bgColor )
 
  410    KNote* note = m_noteList[id];
 
  412        note->setColor( TQColor( fgColor ), TQColor( bgColor ) );
 
  414        kdWarning(5500) << 
"setColor: no note with id: " << 
id << endl;
 
  417int KNotesApp::width( 
const TQString& 
id )
 const 
  419    KNote* note = m_noteList[id];
 
  421        return note->width();
 
  426int KNotesApp::height( 
const TQString& 
id )
 const 
  428    KNote* note = m_noteList[id];
 
  430        return note->height();
 
  435void KNotesApp::move( 
const TQString& 
id, 
int x, 
int y )
 const 
  437    KNote* note = m_noteList[id];
 
  439        return note->move( x, y );
 
  441        kdWarning(5500) << 
"move: no note with id: " << 
id << endl;
 
  444void KNotesApp::resize( 
const TQString& 
id, 
int width, 
int height )
 const 
  446    KNote* note = m_noteList[id];
 
  448        return note->resize( width, height );
 
  450        kdWarning(5500) << 
"resize: no note with id: " << 
id << endl;
 
  453void KNotesApp::sync( 
const TQString& app )
 
  455    TQDictIterator<KNote> it( m_noteList );
 
  457    for ( ; it.current(); ++it )
 
  458        it.current()->sync( app );
 
  461bool KNotesApp::isNew( 
const TQString& app, 
const TQString& 
id )
 const 
  463    KNote* note = m_noteList[id];
 
  465        return note->isNew( app );
 
  470bool KNotesApp::isModified( 
const TQString& app, 
const TQString& 
id )
 const 
  472    KNote* note = m_noteList[id];
 
  474        return note->isModified( app );
 
  482void KNotesApp::mousePressEvent( TQMouseEvent* e )
 
  484    if ( !rect().contains( e->pos() ) )
 
  487    switch ( e->button() )
 
  489    case TQt::LeftButton:
 
  490        if ( m_noteList.count() == 1 )
 
  492            TQDictIterator<KNote> it( m_noteList );
 
  493            showNote( it.toFirst() );
 
  495        else if ( m_note_menu->count() > 0 )
 
  496            m_note_menu->popup( e->globalPos() );
 
  501    case TQt::RightButton:
 
  502        m_context_menu->popup( e->globalPos() );
 
  509void KNotesApp::slotShowNote()
 
  512    showNote( TQString::fromUtf8( sender()->name() ) );
 
  515void KNotesApp::slotWalkThroughNotes()
 
  518    TQDictIterator<KNote> it( m_noteList );
 
  519    KNote *first = it.toFirst();
 
  521        if ( (*it)->hasFocus() )
 
  531void KNotesApp::slotOpenFindDialog()
 
  533    KFindDialog findDia( 
this, 
"find_dialog" );
 
  534    findDia.setHasSelection( 
false );
 
  535    findDia.setHasCursor( 
false );
 
  536    findDia.setSupportsBackwardsFind( 
false );
 
  538    if ( (findDia.exec() != TQDialog::Accepted) || findDia.pattern().isEmpty() )
 
  542    m_findPos = 
new TQDictIterator<KNote>( m_noteList );
 
  546    m_find = 
new KFind( findDia.pattern(), findDia.options(), 
this );
 
  551void KNotesApp::slotFindNext()
 
  555        KNote *note = **m_findPos;
 
  557        note->find( m_find->pattern(), m_find->options() );
 
  561        m_find->displayFinalDialog();
 
  569void KNotesApp::slotPreferences()
 
  572    if ( KNoteConfigDlg::showDialog( 
"KNotes Default Settings" ) )
 
  576    KNoteConfigDlg *dialog = 
new KNoteConfigDlg( 0, i18n(
"Settings"), 
this,
 
  578    connect( dialog, TQ_SIGNAL(settingsChanged()), 
this, TQ_SLOT(updateNetworkListener()) );
 
  579    connect( dialog, TQ_SIGNAL(settingsChanged()), 
this, TQ_SLOT(updateStyle()) );
 
  583void KNotesApp::slotConfigureAccels()
 
  585    KNotesKeyDialog keys( m_globalAccel, 
this );
 
  586    TQDictIterator<KNote> notes( m_noteList );
 
  587    if ( !m_noteList.isEmpty() )
 
  588        keys.insert( (*notes)->actionCollection() );
 
  591    m_globalAccel->writeSettings();
 
  592    updateGlobalAccels();
 
  595    m_noteGUI.setContent(
 
  596        KXMLGUIFactory::readConfigFile( instance()->instanceName() + 
"ui.rc", instance() )
 
  599    if ( m_noteList.isEmpty() )
 
  603    TQValueList<TDEAction *> list = (*notes)->actionCollection()->actions();
 
  604    for ( TQValueList<TDEAction *>::iterator it = list.begin(); it != list.end(); ++it )
 
  607        for ( ++notes; *notes; ++notes )
 
  609            TDEAction *toChange = (*notes)->actionCollection()->action( (*it)->name() );
 
  610            if ( toChange->shortcut() != (*it)->shortcut() )
 
  611                toChange->setShortcut( (*it)->shortcut() );
 
  616void KNotesApp::slotNoteKilled( KCal::Journal *journal )
 
  619    m_manager->deleteNote( journal );
 
  623void KNotesApp::slotQuit()
 
  625    TQDictIterator<KNote> it( m_noteList );
 
  628        if ( (*it)->isModified() )
 
  629            (*it)->saveData(
false);
 
  638void KNotesApp::showNote( KNote* note )
 const 
  641    KWin::setCurrentDesktop( KWin::windowInfo( note->winId() ).desktop() );
 
  642    KWin::forceActiveWindow( note->winId() );
 
  646void KNotesApp::createNote( KCal::Journal *journal )
 
  648  if( journal->uid() == m_noteUidModify)
 
  650         KNote *note = m_noteList[m_noteUidModify];
 
  652                 note->changeJournal(journal);
 
  656  m_noteUidModify = journal->uid();
 
  657    KNote *newNote = 
new KNote( m_noteGUI, journal, 0, journal->uid().utf8() );
 
  658    m_noteList.insert( newNote->noteId(), newNote );
 
  660    connect( newNote, TQ_SIGNAL(sigRequestNewNote()), TQ_SLOT(newNote()) );
 
  661    connect( newNote, TQ_SIGNAL(sigShowNextNote()), TQ_SLOT(slotWalkThroughNotes()) );
 
  662    connect( newNote, TQ_SIGNAL(sigKillNote( KCal::Journal* )),
 
  663                        TQ_SLOT(slotNoteKilled( KCal::Journal* )) );
 
  664    connect( newNote, TQ_SIGNAL(sigNameChanged()), TQ_SLOT(updateNoteActions()) );
 
  665    connect( newNote, TQ_SIGNAL(sigDataChanged(
const TQString &)), TQ_SLOT(saveNotes(
const TQString &)) );
 
  666    connect( newNote, TQ_SIGNAL(sigColorChanged()), TQ_SLOT(updateNoteActions()) );
 
  667    connect( newNote, TQ_SIGNAL(sigFindFinished()), TQ_SLOT(slotFindNext()) );
 
  674void KNotesApp::killNote( KCal::Journal *journal )
 
  676  if(m_noteUidModify == journal->uid())
 
  681    KNote *note = m_noteList.take( journal->uid() );
 
  684        note->deleteWhenIdle();
 
  689void KNotesApp::acceptConnection()
 
  692    TDEBufferedSocket *s = 
static_cast<TDEBufferedSocket *
>(m_listener->accept());
 
  695        KNotesNetworkReceiver *recv = 
new KNotesNetworkReceiver( s );
 
  696        connect( recv, TQ_SIGNAL(sigNoteReceived( 
const TQString &, 
const TQString & )),
 
  697                 this, TQ_SLOT(newNote( 
const TQString &, 
const TQString & )) );
 
  701void KNotesApp::saveNotes( 
const TQString & uid )
 
  703  m_noteUidModify = uid;
 
  707void KNotesApp::saveNotes()
 
  709    KNotesGlobalConfig::writeConfig();
 
  713void KNotesApp::saveConfigs()
 
  715    TQDictIterator<KNote> it( m_noteList );
 
  716    for ( ; it.current(); ++it )
 
  717        it.current()->saveConfig();
 
  720void KNotesApp::updateNoteActions()
 
  722    unplugActionList( 
"notes" );
 
  723    m_noteActions.clear();
 
  725    for ( TQDictIterator<KNote> it( m_noteList ); it.current(); ++it )
 
  727        TDEAction *action = 
new TDEAction( it.current()->name().replace(
"&", 
"&&"),
 
  728                                       TDEShortcut(), 
this, TQ_SLOT(slotShowNote()),
 
  730                                       it.current()->noteId().utf8() );
 
  731        TDEIconEffect effect;
 
  732        TQPixmap icon = effect.apply( tdeApp->miniIcon(), TDEIconEffect::Colorize, 1,
 
  733                                     it.current()->paletteBackgroundColor(), 
false );
 
  734        action->setIconSet( icon );
 
  735        m_noteActions.append( action );
 
  738    if ( m_noteActions.isEmpty() )
 
  740        actionCollection()->action( 
"hide_all_notes" )->setEnabled( 
false );
 
  741        actionCollection()->action( 
"show_all_notes" )->setEnabled( 
false );
 
  742        m_findAction->setEnabled( 
false );
 
  743        TDEAction *action = 
new TDEAction( i18n(
"No Notes") );
 
  744        m_noteActions.append( action );
 
  748        actionCollection()->action( 
"hide_all_notes" )->setEnabled( 
true );
 
  749        actionCollection()->action( 
"show_all_notes" )->setEnabled( 
true );
 
  750        m_findAction->setEnabled( 
true );
 
  751        m_noteActions.sort();
 
  753    plugActionList( 
"notes", m_noteActions );
 
  756void KNotesApp::updateGlobalAccels()
 
  758    if ( m_globalAccel->isEnabled() )
 
  760        TDEAction *action = actionCollection()->action( 
"new_note" );
 
  762            action->setShortcut( m_globalAccel->shortcut( 
"global_new_note" ) );
 
  763        action = actionCollection()->action( 
"new_note_clipboard" );
 
  765            action->setShortcut( m_globalAccel->shortcut( 
"global_new_note_clipboard" ) );
 
  766        action = actionCollection()->action( 
"hide_all_notes" );
 
  768            action->setShortcut( m_globalAccel->shortcut( 
"global_hide_all_notes" ) );
 
  769        action = actionCollection()->action( 
"show_all_notes" );
 
  771            action->setShortcut( m_globalAccel->shortcut( 
"global_show_all_notes" ) );
 
  773        m_globalAccel->updateConnections();
 
  777        TDEAction *action = actionCollection()->action( 
"new_note" );
 
  779            action->setShortcut( 0 );
 
  780        action = actionCollection()->action( 
"new_note_clipboard" );
 
  782            action->setShortcut( 0 );
 
  783        action = actionCollection()->action( 
"hide_all_notes" );
 
  785            action->setShortcut( 0 );
 
  786        action = actionCollection()->action( 
"show_all_notes" );
 
  788            action->setShortcut( 0 );
 
  792void KNotesApp::updateNetworkListener()
 
  796    if ( KNotesGlobalConfig::receiveNotes() )
 
  798        m_listener->setAddress( TQString::number( KNotesGlobalConfig::port() ) );
 
  800        m_listener->listen();
 
  804void KNotesApp::updateStyle()
 
  806    KNote::setStyle( KNotesGlobalConfig::style() );
 
  808    TQDictIterator<KNote> it( m_noteList );
 
  809    for ( ; it.current(); ++it )
 
  810        TQApplication::postEvent( *it, 
new TQEvent( TQEvent::LayoutHint ) );
 
  813#include "knotesapp.moc"