summaryrefslogtreecommitdiffstats
path: root/kate/app/katesession.h
blob: 3351db150aa09f8b80a092ef921957f05240c292 (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
/* This file is part of the KDE project
   Copyright (C) 2015-2016 Michele Calgaro <micheleDOTcalgaro__AT__yahooDOTit>
   partially based on previous work from
   Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

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

#ifndef __KATE_SESSION_H__
#define __KATE_SESSION_H__

#include "katemain.h"

#include <kdialogbase.h>
#include <ksimpleconfig.h>
#include <ksharedptr.h>
#include <tdeaction.h>

#include <tqobject.h>
#include <tqptrlist.h>
#include <tqvaluelist.h>
#include <tqstringlist.h>
#include <tdelistview.h>

class KateViewSpace;
class KDirWatch;
class KPushButton;
class TDEListView;
class TQCheckBox;


//BEGIN KateSession
/**
 * An object representing a Kate's session.
 */
class KateSession
{
	public:

		/**
		 * create a new session and read the config from fileName if it exists
		 * @param sessionName session name
		 * @param fileName file where session config is saved to/restored from
		 * @param isFullName true  -> filename is a full filename, used to load/save the session configuration
		 *                   false -> filename is a folder name. This is used for new unsaved sessions
		 *                            to inject the location where the configuration file should be saved
		 */
		KateSession(const TQString &sessionName, const TQString &fileName, bool isFullName);

		/**
		 * Destructor
		 */
		~KateSession();

		/**
		* @return the session name
		*/
		const TQString& getSessionName() const { return m_sessionName; }

		/**
		* Set the new session name
		* @param sessionName the new session name
		*/
		void setSessionName(const TQString &sessionName);

		/**
		* @return whether the session is read only or not
		*/
		bool isReadOnly() const { return m_readOnly; }

		/**
		* Set session read only status
		* @param readOnly if true, the session status can not be modified
		*/
		void setReadOnly(bool readOnly) { m_readOnly = readOnly; }

		/**
		* @return the session filename if available, otherwise the null string
		*/
		const TQString& getSessionFilename() const { return m_isFullName ? m_filename : TQString::null; }

		/**
		* @return the number of documents in the session
		*/
		int getDocCount() const { return m_documents.count(); }

		/**
		 * Save session info
		 * @param saveGUIInfo if true, save also the information about the GUI elements
		 * @param setReadOnly necessary to save a session that has to be turned to read only
		 */
		void save(bool saveGUIInfo, bool setReadOnly = false);

		/**
		 * Activate the session
		 */
		void activate();


	private:

		friend class KateViewSpace;

		/**
		* @return the session config object
		*/
		TDEConfig* getConfig() const { return m_config; }


		TQString       m_sessionName;
		TQString       m_filename;
		bool           m_isFullName;   // true  -> m_filename is a full filename
																	 // false -> m_filename is a folder name.
		bool           m_readOnly;
		TQStringList   m_documents;    // document URLs
		KSimpleConfig *m_config;       // session config

};
//END KateSession


//BEGIN KateSessionManager
//FIXME (advanced)
//There should be only one session manager regardless of how many instances of Kate are running.
//Changes should propagate to all session panels. Different Kate instances should run different
//sessions. If the user switches to a session already opened in another Kate instance, the current
//session should be saved and then the focus switched to the other instance.
//This would allow a safe use of multiple Kate instances without overwriting session information
//among them. Currently the last instance to be closed will overwrite the information previously
//saved by other Kate instances.
/**
 * The Kate session manager. It takes care of storing and retrieving each session object
 * as well as providing methods to operate on them.
 *
 * @note The Kate session manager takes ownership of each session object it handles.
 */
class KateSessionManager : public TQObject
{
  Q_OBJECT

	public:

    enum
    {
      INVALID_SESSION = -1
    };

		/**
		 * get a pointer to the unique KateSessionManager instance.
		 * If the manager does not exist yet, create it.
		 */
		static KateSessionManager* self();

		/**
		 * Destructor
		 */
		~KateSessionManager();

		/**
		 * Save session manager info
		 */
		void saveConfig();

		/**
		 * @return the active session id
		 */
		int getActiveSessionId() const { return m_activeSessionId; }

		/**
		 * @return the active session name
		 */
		const TQString& getActiveSessionName() /*FIXME const*/ { return m_sessions[m_activeSessionId]->getSessionName(); }

		/**
		 * @return a reference to the active session
		 */
		KateSession* getActiveSession() { return m_sessions[m_activeSessionId]; }

		/**
		 * @param sessionId the id of the session to return
		 * @return a reference to the specified session
		 */
		KateSession* getSessionFromId(int sessionId);

    /**
     * Return the session id of the first session whose name matches the
     * provided one. In case multiple sessions share the same name,
     * the id of the first one found will be returned.
     * @param name the session name to look for
     * @return the session id of the matching session if it is found,
     *         otherwise KateSessionManager::INVALID_SESSION.
     */
    int getSessionIdFromName(const TQString &name);

		/**
		 * @return a reference to the sessions list
		 */
		TQPtrList<KateSession>& getSessionsList() { return m_sessions; }

		/**
		 * Activate the selected session.
		 * @param sessionId the id of the session to activate
		 * @param saveCurr if true, save the current session before activating the new one
		 * @return whether the session was activated or not
		 * @emit sessionActivated
		 */
		bool activateSession(int sessionId, bool saveCurr = true);

		/**
		 * Create a new session and activate it if required
		 * @param sessionName new session name
		 * @param activate if true, activate the new session after creation
		 * @return the id of the newly created session
		 * @emit sessionCreated
		 */
		int newSession(const TQString &sessionName = TQString::null, bool activate = true);

    /**
     * Restore the last saved session. Can only be used before
     * any other session has been activated, i.e. on Kate's startup
     * @return whether the session was activated or not
     */
    bool restoreLastSession();

		/**
		 * Saves the active session
     * @emit sessionSaved (through invoked "void saveSession(int)" method)
		 */
		void saveActiveSession() { saveSession(m_activeSessionId); }

		/**
		 * Save the specified session
		 * @param sessionId the id of the session to save
     * @emit sessionSaved
     */
		void saveSession(int sessionId);

		/**
		 * Delete the specified session
		 * @param sessionId the id of the session to delete
     * @return whether the session has been deleted or not
     * @emit sessionDeleted
		 */
		bool deleteSession(int sessionId);

		/**
		 * Move the specified session forward in the session list (by one position)
		 * @param sessionId the id of the session to move
     */
		void moveSessionForward(int sessionId);

		/**
		 * Move the specified session backward in the session list (by one position)
		 * @param sessionId the id of the session to move
     */
		void moveSessionBackward(int sessionId);

		/**
		 * Rename the specified session
		 * @param sessionId the id of the session to rename
		 * @param newSessionName the new session name
     */
		void renameSession(int sessionId, const TQString &newSessionName);

		/**
		 * Set the read only status of the specified session
		 * @param sessionId the id of the session to modify
		 * @param readOnly the new read only status
		 */
		void setSessionReadOnlyStatus(int sessionId, bool readOnly);


  signals:
		/**
		 * Emitted once a session has been activated
		 * @param newSessionId the id of the previous active session
		 * @param oldSessionId the id of the new active session
		 */
		void sessionActivated(int newSessionId, int oldSessionId);

		/**
		 * Emitted once a session has been created
		 * @param sessionId the id of the new session
		 */
		void sessionCreated(int sessionId);

		/**
		 * Emitted once a session has been saved
		 * @param sessionId the id of the saved session
		 */
		void sessionSaved(int sessionId);

		/**
		 * Emitted once a session has been deleted
		 * @param sessionId the id of the deleted session
		 */
		void sessionDeleted(int sessionId);

		/**
		 * Emitted once the position of the two sessions have been swapped
		 * @param sessionIdMin the smallest id of the session couple
		 * @param sessionIdMax the biggest id of the session couple
		 */
		void sessionsSwapped(int sessionIdMin, int sessionIdMax);


	protected:
		KateSessionManager();

		/**
		 * Swap the position of the two specified sessions in the session list
		 * @param sessionId1 the id of the first session
		 * @param sessionId2 the id of the second session
		 * @emit sessionsSwapped
		 */
		void swapSessionsPosition(int sessionId1, int sessionId2);


		TQString m_baseDir;       					// folder where session files are stored
		TQString m_configFile;     					// file where the session list config is stored
		int m_activeSessionId;              // index of the active session
    bool m_firstActivation;             // true until at least one session has been activated
		TQPtrList<KateSession> m_sessions;  // session list
		KSimpleConfig *m_config;        		// session manager config

		static KateSessionManager *ksm_instance;  // the only KateSessionManager instance
};
//END KateSessionManager


//BEGIN KateSessionChooserItem
class KateSessionChooserItem : public TDEListViewItem
{
  public:
    KateSessionChooserItem(TQListView *listview, const TQString &sessionName, const TQString &nDoc, int sessionId)
      : TDEListViewItem(listview, sessionName, nDoc), m_sessionId(sessionId) {}

		int  getSessionId() { return m_sessionId; }
		void setSessionId(int sessionId) { m_sessionId = sessionId; }

	protected:
    int m_sessionId;
};
//END KateSessionChooserItem


//BEGIN KateSessionChooser
class KateSessionChooser : public KDialogBase
{
	Q_OBJECT

	public:
		enum Result
		{
			RESULT_NO_OP = TQDialog::Rejected,
			RESULT_OPEN_EXISTING,
			RESULT_OPEN_NEW,
			RESULT_QUIT_KATE
		};

		KateSessionChooser(TQWidget *parent);
	 ~KateSessionChooser() {}

		int getSelectedSessionId();  // return the session id of the selected session

	protected slots:

		void slotUser1();   // open existing session
		void slotUser2();   // open new session
		void slotUser3();   // quit kate
		void slotSelectionChanged();  // list selection has changed

	protected:
		TDEListView *m_listview;
};
//BEGIN KateSessionChooser




//------------------------------------
//------------------------------------
//------------------------------------
class OldKateSessionManager;
class OldKateSession  : public TDEShared
{
	public:
		/**
		 * Define a Shared-Pointer type
		 */
		typedef TDESharedPtr<OldKateSession> Ptr;

	public:
		/**
		 * create a session from given file
		 * @param fileName session filename, relative
		 * @param name session name
		 * @param manager pointer to the manager
		 */
		OldKateSession ( OldKateSessionManager *manager, const TQString &fileName, const TQString &name );

		/**
		 * init the session object, after construction or create
		 */
		void init ();

		/**
		 * destruct me
		 */
		~OldKateSession ();

		/**
		 * session filename, absolute, calculated out of relative filename + session dir
		 * @return absolute path to session file
		 */
		TQString sessionFile () const;

		/**
		 * relative session filename
		 * @return relative filename for this session
		 */
		const TQString &sessionFileRelative () const { return m_sessionFileRel; }

		/**
		 * session name
		 * @return name for this session
		 */
		const TQString &sessionName () const { return m_sessionName; }

		/**
		 * is this a valid session? if not, don't use any session if this is
		 * the active one
		 */
		bool isNew () const { return m_sessionName.isEmpty(); }

		/**
		 * create the session file, if not existing
		 * @param name name for this session
		 * @param force force to create new file
		 * @return true if created, false if no creation needed
		 */
		bool create ( const TQString &name, bool force = false );

		/**
		 * rename this session
		 * @param name new name
		 * @return success
		 */
		bool rename ( const TQString &name );

		/**
		 * config to read
		 * on first access, will create the config object, delete will be done automagic
		 * return 0 if we have no file to read config from atm
		 * @return config to read from
		 */
		TDEConfig *configRead ();

		/**
		 * config to write
		 * on first access, will create the config object, delete will be done automagic
		 * return 0 if we have no file to write config to atm
		 * @return config to write from
		 */
		TDEConfig *configWrite ();

		/**
		 * count of documents in this session
		 * @return documents count
		 */
		unsigned int documents () const { return m_documents; }

	private:
		/**
		 * session filename, in local location we can write to
		 * relative filename to the session dirs :)
		 */
		TQString m_sessionFileRel;

		/**
		 * session name, extracted from the file, to display to the user
		 */
		TQString m_sessionName;

		/**
		 * number of document of this session
		 */
		unsigned int m_documents;

		/**
		 * OldKateSessionMananger
		 */
		OldKateSessionManager *m_manager;

		/**
		 * simpleconfig to read from
		 */
		KSimpleConfig *m_readConfig;

		/**
		 * simpleconfig to write to
		 */
		KSimpleConfig *m_writeConfig;
};

typedef TQValueList<OldKateSession::Ptr> OldKateSessionList;

class OldKateSessionManager : public TQObject
{
		Q_OBJECT

	public:
		OldKateSessionManager ( TQObject *parent );
		~OldKateSessionManager();

		/**
		 * allow access to this :)
		 * @return instance of the session manager
		 */
		static OldKateSessionManager *self();

		/**
		 * allow access to the session list
		 * kept up to date by watching the dir
		 */
		inline OldKateSessionList & sessionList () { updateSessionList (); return m_sessionList; }

		/**
		 * activate a session
		 * first, it will look if a session with this name exists in list
		 * if yes, it will use this session, else it will create a new session file
		 * @param session session to activate
		 * @param closeLast try to close last session or not?
		 * @param saveLast try to save last session or not?
		 * @param loadNew load new session stuff?
		 */
		void activateSession ( OldKateSession::Ptr session, bool closeLast = true, bool saveLast = true, bool loadNew = true );

		/**
		 * create a new session
		 * @param name session name
		 */
		OldKateSession::Ptr createSession ( const TQString &name );

		/**
		 * return session with given name
		 * if no existing session matches, create new one with this name
		 * @param name session name
		 */
		OldKateSession::Ptr giveSession ( const TQString &name );

		/**
		 * save current session
		 * for sessions without filename: save nothing
		 * @param tryAsk should we ask user if needed?
		 * @param rememberAsLast remember this session as last used?
		 * @return success
		 */
		bool saveActiveSession ( bool tryAsk = false, bool rememberAsLast = false );

		/**
		 * return the current active session
		 * sessionFile == empty means we have no session around for this instance of kate
		 * @return session active atm
		 */
		inline OldKateSession::Ptr activeSession () { return m_activeSession; }

		/**
		 * session dir
		 * @return global session dir
		 */
		inline const TQString &sessionsDir () const { return m_sessionsDir; }

		/**
		 * initial session chooser, on app start
		 * @return success, if false, app should exit
		 */
		bool chooseSession ();

	public slots:
		/**
		 * try to start a new session
		 * asks user first for name
		 */
		void sessionNew ();

		/**
		 * try to open a existing session
		 */
		void sessionOpen ();

		/**
		 * try to save current session
		 */
		void sessionSave ();

		/**
		 * try to save as current session
		 */
		void sessionSaveAs ();

		/**
		 * show dialog to manage our sessions
		 */
		void sessionManage ();

	private slots:
		void dirty ( const TQString &path );

	public:
		/**
		 * trigger update of session list
		 */
		void updateSessionList ();

	private:
		/**
		 * absolute path to dir in home dir where to store the sessions
		 */
		TQString m_sessionsDir;

		/**
		 * list of current available sessions
		 */
		OldKateSessionList m_sessionList;

		/**
		 * current active session
		 */
		OldKateSession::Ptr m_activeSession;
};


class OldKateSessionOpenDialog : public KDialogBase
{
		Q_OBJECT

	public:
		OldKateSessionOpenDialog ( TQWidget *parent );
		~OldKateSessionOpenDialog ();

		OldKateSession::Ptr selectedSession ();

		enum
		{
			resultOk,
			resultCancel
		};

	protected slots:
		/**
		 * cancel pressed
		 */
		void slotUser1 ();

		/**
		 * ok pressed
		 */
		void slotUser2 ();

	private:
		TDEListView *m_sessions;
};

class OldKateSessionManageDialog : public KDialogBase
{
		Q_OBJECT

	public:
		OldKateSessionManageDialog ( TQWidget *parent );
		~OldKateSessionManageDialog ();

	protected slots:
		/**
		 * close pressed
		 */
		void slotUser1 ();

		/**
		 * selection has changed
		 */
		void selectionChanged ();

		/**
		 * try to rename session
		 */
		void rename ();

		/**
		 * try to delete session
		 */
		void del ();

	private:
		/**
		 * update our list
		 */
		void updateSessionList ();

	private:
		TDEListView *m_sessions;
		KPushButton *m_rename;
		KPushButton *m_del;
};

class OldKateSessionsAction : public TDEActionMenu
{
		Q_OBJECT

	public:
		OldKateSessionsAction ( const TQString& text, TQObject* parent = 0, const char* name = 0 );
		~OldKateSessionsAction () {;};

	public  slots:
		void slotAboutToShow();

		void openSession ( int i );
};

#endif