1/* This file is part of the KDE project
2 Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#ifndef __KATE_SESSION_H__
20#define __KATE_SESSION_H__
21
22#include "katemain.h"
23
24#include <KDialog>
25#include <KConfig>
26#include <KSharedPtr>
27#include <KActionMenu>
28
29#include <QObject>
30#include <QList>
31#include <QActionGroup>
32#include <QTreeWidget>
33#include <QTreeWidgetItem>
34
35class KateSessionManager;
36class KPushButton;
37
38class QCheckBox;
39
40class KateSession : public KShared
41{
42 public:
43 /**
44 * Define a Shared-Pointer type
45 */
46 typedef KSharedPtr<KateSession> Ptr;
47
48 public:
49 /**
50 * create a session from given file
51 * @param manager pointer to the manager
52 * @param fileName session filename, relative
53 */
54 KateSession (KateSessionManager *manager, const QString &fileName);
55
56 /**
57 * init the session object, after construction or create
58 */
59 void init ();
60
61 /**
62 * destruct me
63 */
64 ~KateSession ();
65
66 /**
67 * session filename, absolute, calculated out of relative filename + session dir
68 * @return absolute path to session file
69 */
70 QString sessionFile () const;
71
72 /**
73 * relative session filename
74 * @return relative filename for this session
75 */
76 const QString &sessionFileRelative () const
77 {
78 return m_sessionFileRel;
79 }
80
81 /**
82 * session name
83 * @return name for this session
84 */
85 const QString &sessionName () const
86 {
87 return m_sessionName;
88 }
89
90 bool isAnonymous() const
91 {
92 return !(m_readConfig && m_writeConfig);
93 }
94
95 void makeAnonymous();
96
97 /**
98 * create the session file, if not existing
99 * @param name name for this session
100 * @param force force to create new file
101 * @return true if created, false if no creation needed
102 */
103 bool create (const QString &name, bool force = false);
104
105 /**
106 * rename this session
107 * @param name new name
108 * @return success
109 */
110 bool rename (const QString &name);
111
112 /**
113 * config to read
114 * on first access, will create the config object, delete will be done automagic
115 * return 0 if we have no file to read config from atm
116 * @return config to read from
117 * @note never delete configRead(), because the return value might be
118 * KGlobal::config(). Only delete the member variables directly.
119 */
120 KConfig *configRead ();
121
122 /**
123 * config to write
124 * on first access, will create the config object, delete will be done automagic
125 * return 0 if we have no file to write config to atm
126 * @return config to write from
127 * @note never delete configWrite(), because the return value might be
128 * KGlobal::config(). Only delete the member variables directly.
129 */
130 KConfig *configWrite ();
131
132 /**
133 * count of documents in this session
134 * @return documents count
135 */
136 unsigned int documents () const
137 {
138 return m_documents;
139 }
140
141 private:
142 /**
143 * session filename, in local location we can write to
144 * relative filename to the session dirs :)
145 */
146 QString m_sessionFileRel;
147
148 /**
149 * session name, extracted from the file, to display to the user
150 */
151 QString m_sessionName;
152
153 /**
154 * number of document of this session
155 */
156 unsigned int m_documents;
157
158 /**
159 * KateSessionMananger
160 */
161 KateSessionManager *m_manager;
162
163 /**
164 * simpleconfig to read from
165 */
166 KConfig *m_readConfig;
167
168 /**
169 * simpleconfig to write to
170 */
171 KConfig *m_writeConfig;
172};
173
174typedef QList<KateSession::Ptr> KateSessionList;
175
176class KateSessionManager : public QObject
177{
178 Q_OBJECT
179
180 public:
181 KateSessionManager(QObject *parent);
182 ~KateSessionManager();
183
184 /**
185 * allow access to this :)
186 * @return instance of the session manager
187 */
188 static KateSessionManager *self();
189
190 /**
191 * allow access to the session list
192 * kept up to date by watching the dir
193 */
194 inline KateSessionList & sessionList ()
195 {
196 updateSessionList ();
197 return m_sessionList;
198 }
199
200 /**
201 * activate a session
202 * first, it will look if a session with this name exists in list
203 * if yes, it will use this session, else it will create a new session file
204 * @param session session to activate
205 * @param closeLast try to close last session or not?
206 * @param saveLast try to save last session or not?
207 * @param loadNew load new session stuff?
208 * @return false==session has been delegated, true==session has been activated in this distance
209 */
210 bool activateSession (KateSession::Ptr session, bool closeLast = true, bool saveLast = true, bool loadNew = true);
211
212 /**
213 * return session with given name
214 * if no existing session matches, create new one with this name
215 * @param name session name
216 */
217 KateSession::Ptr giveSession (const QString &name);
218
219 /**
220 * save current session
221 * for sessions without filename: save nothing
222 * @param rememberAsLast remember this session as last used?
223 * @return success
224 */
225 bool saveActiveSession (bool rememberAsLast = false);
226
227 /**
228 * return the current active session
229 * sessionFile == empty means we have no session around for this instance of kate
230 * @return session active atm
231 */
232 inline KateSession::Ptr activeSession ()
233 {
234 return m_activeSession;
235 }
236
237 /**
238 * session dir
239 * @return global session dir
240 */
241 inline const QString &sessionsDir () const
242 {
243 return m_sessionsDir;
244 }
245
246 /**
247 * initial session chooser, on app start
248 * @return success, if false, app should exit
249 */
250 bool chooseSession ();
251
252 public Q_SLOTS:
253 /**
254 * try to start a new session
255 * asks user first for name
256 */
257 void sessionNew ();
258
259 /**
260 * try to open a existing session
261 */
262 void sessionOpen ();
263
264 /**
265 * try to save current session
266 */
267 void sessionSave ();
268
269 /**
270 * try to save as current session
271 */
272 void sessionSaveAs ();
273
274 /**
275 * show dialog to manage our sessions
276 */
277 void sessionManage ();
278
279 Q_SIGNALS:
280 /**
281 * Emitted, whenever the session changes, e.g. when it was renamed.
282 */
283 friend class KateSessionManageDialog;
284 void sessionChanged();
285
286 private Q_SLOTS:
287 void dirty (const QString &path);
288
289 public:
290 /**
291 * trigger update of session list
292 */
293 void updateSessionList ();
294
295 private:
296 /**
297 * Asks the user for a new session name. Used by save as for example.
298 */
299 bool newSessionName();
300
301 private:
302 /**
303 * absolute path to dir in home dir where to store the sessions
304 */
305 QString m_sessionsDir;
306
307 /**
308 * list of current available sessions
309 */
310 KateSessionList m_sessionList;
311
312 /**
313 * current active session
314 */
315 KateSession::Ptr m_activeSession;
316};
317
318class KateSessionChooser : public KDialog
319{
320 Q_OBJECT
321
322 public:
323 KateSessionChooser (QWidget *parent, const QString &lastSession);
324 ~KateSessionChooser ();
325
326 KateSession::Ptr selectedSession ();
327 bool reopenLastSession ();
328
329 enum {
330 resultQuit = QDialog::Rejected,
331 resultOpen,
332 resultNew,
333 resultNone,
334 resultCopy
335 };
336
337 protected Q_SLOTS:
338 /**
339 * quit kate
340 */
341 void slotUser1 ();
342
343 /**
344 * open session
345 */
346 void slotUser2 ();
347
348 /**
349 * new session
350 */
351 void slotUser3 ();
352
353 void slotCopySession();
354
355 /**
356 * selection has changed
357 */
358 void selectionChanged (QTreeWidgetItem *current, QTreeWidgetItem *previous);
359
360 private:
361 QTreeWidget *m_sessions;
362 QCheckBox *m_useLast;
363};
364
365class KateSessionOpenDialog : public KDialog
366{
367 Q_OBJECT
368
369 public:
370 KateSessionOpenDialog (QWidget *parent);
371 ~KateSessionOpenDialog ();
372
373 KateSession::Ptr selectedSession ();
374
375 enum {
376 resultOk,
377 resultCancel
378 };
379
380 protected Q_SLOTS:
381 /**
382 * cancel pressed
383 */
384 void slotUser1 ();
385
386 /**
387 * ok pressed
388 */
389 void slotUser2 ();
390
391 /**
392 * selection has changed
393 */
394 void selectionChanged (QTreeWidgetItem *current, QTreeWidgetItem *previous);
395
396 private:
397 QTreeWidget *m_sessions;
398};
399
400class KateSessionManageDialog : public KDialog
401{
402 Q_OBJECT
403
404 public:
405 KateSessionManageDialog (QWidget *parent);
406 ~KateSessionManageDialog ();
407
408 protected Q_SLOTS:
409 /**
410 * close pressed
411 */
412 void slotUser1 ();
413
414 /**
415 * selection has changed
416 */
417 void selectionChanged (QTreeWidgetItem *current, QTreeWidgetItem *previous);
418
419 /**
420 * try to rename session
421 */
422 void rename ();
423
424 /**
425 * try to delete session
426 */
427 void del ();
428
429 /**
430 * close dialog and open the selected session
431 */
432 void open ();
433
434 private:
435 /**
436 * update our list
437 */
438 void updateSessionList ();
439
440 private:
441 QTreeWidget *m_sessions;
442 KPushButton *m_rename;
443 KPushButton *m_del;
444};
445
446class KateSessionsAction : public KActionMenu
447{
448 Q_OBJECT
449
450 public:
451 KateSessionsAction(const QString& text, QObject *parent);
452 ~KateSessionsAction ()
453 {
454 }
455
456 public Q_SLOTS:
457 void slotAboutToShow();
458
459 void openSession (QAction *action);
460 void slotSessionChanged();
461
462 private:
463 QActionGroup *sessionsGroup;
464};
465
466#endif
467
468// kate: space-indent on; indent-width 2; replace-tabs on;
469