summaryrefslogtreecommitdiffstats
path: root/kicker/menuext/kate/katesessionmenu.cpp
blob: 032a2ab050d0ad5478d19d5d3c7f1ff62dde7534 (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
/*
    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.

    ---
    Copyright (C) 2005, Anders Lund <[email protected]>
 */

#include "katesessionmenu.h"
#include "katesessionmenu.moc"

#include <kapplication.h>
#include <kconfig.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <klibloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>

#include <tqvalidator.h>

class Validator : public TQValidator {
  public:
    Validator( TQObject *parent ) : TQValidator( parent, 0 ) {}
    ~Validator() {}
    virtual TQValidator::State validate( TQString &, int & ) const { return TQValidator::Acceptable; }
};

K_EXPORT_KICKER_MENUEXT(kate, KateSessionMenu)

KateSessionMenu::KateSessionMenu( TQWidget *parent, const char *name, const TQStringList& )
  : KPanelMenu( parent, name ),
    m_parent( parent )
{
}

KateSessionMenu::~KateSessionMenu()
{
}

// update the session list and rebuild the menu
void KateSessionMenu::initialize()
{
  if ( initialized() )
  {
    return;
  }

  m_sessions.clear();

  int id = 0;

  // no session - exec 'kate'
  insertItem( SmallIconSet("kate"), i18n("Start Kate (no arguments)"), id++ );

  // new session - prompt for a name and  exec 'kate --start NAME'
  insertItem( SmallIconSet("new"), i18n("New Kate Session"), id++ );

  // new anonymous session, 'kate --start ""'
  insertItem( SmallIconSet("new"), i18n("New Anonymous Session"), id++ );

  insertSeparator();

  TQStringList list = TDEGlobal::dirs()->findAllResources( "data", "kate/sessions/*.katesession", false, true);
  for (TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
  {
    KSimpleConfig config( *it, true );
    config.setGroup( "General" );
    m_sessions.append( config.readEntry( "Name" ) );
  }

  m_sessions.sort();

  for ( TQStringList::ConstIterator it1 = m_sessions.begin(); it1 != m_sessions.end(); ++it1 )
  {
    insertItem( *it1, id++ );
  }

  // means for updating, to let the user manually update if he/she added new sessions.
  insertSeparator();
  insertItem( SmallIconSet("reload"), i18n("Reload Session List"), this, TQT_SLOT(reinitialize()) );
}

void KateSessionMenu::slotExec( int id )
{
  if ( id < 0 )
    return;

  TQStringList args;
  if ( id > 0 )
    args << "--start";

  // If a new session is requested we try to ask for a name.
  if ( id == 1 )
  {
    bool ok (false);
    TQString name = KInputDialog::getText( i18n("Session Name"),
                                          i18n("Please enter a name for the new session"),
                                          TQString::null,
                                          &ok, 0, 0, new Validator( TQT_TQOBJECT(m_parent) ) );
    if ( ! ok )
      return;

    if ( name.isEmpty() && KMessageBox::questionYesNo( 0,
                          i18n("An unnamed session will not be saved automatically. "
                               "Do you want to create such a session?"),
                          i18n("Create anonymous session?"),
                          KStdGuiItem::yes(), KStdGuiItem::cancel(),
                          "kate_session_button_create_anonymous" ) == KMessageBox::No )
      return;

    if ( m_sessions.contains( name ) &&
         KMessageBox::warningYesNo( 0,
                                    i18n("You allready have a session named %1. Do you want to open that session?").arg( name ),
                                    i18n("Session exists") ) == KMessageBox::No )
      return;
    else
      // mark the menu as dirty so that it gets rebuild at next display
      // to show the new session
      setInitialized( false );

    args << name;
  }

  else if ( id == 2 )
    args << "";

  else if ( id > 2 )
    args << m_sessions[ id-3 ];

  kapp->tdeinitExec("kate", args);
}


// kate: space-indent: on; indent-width 2; replace-tabs on;