summaryrefslogtreecommitdiffstats
path: root/kate/filetemplates/plugin/filetemplates.h
blob: a9d53f299ce6cb0b3a79a131caced82373fd1e7a (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
/*
    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) 2004, Anders Lund <anders@alweb.dk>
 */

#ifndef _PLUGIN_KATE_FILETEMPLATES_H_
#define _PLUGIN_KATE_FILETEMPLATES_H_

#include <kate/application.h>
#include <kate/documentmanager.h>
#include <kate/document.h>
#include <kate/mainwindow.h>
#include <kate/plugin.h>
#include <kate/view.h>
#include <kate/viewmanager.h>

#include <klibloader.h>
#include <klocale.h>
#include <kurl.h>
#include <kwizard.h>

#include <tqptrlist.h>

class KatePluginFactory : public KLibFactory
{
  Q_OBJECT
  

  public:
    KatePluginFactory();
    virtual ~KatePluginFactory();

    virtual TQObject* createObject( TQObject* parent = 0, const char* pname = 0, const char* name = TQOBJECT_OBJECT_NAME_STRING, const TQStringList &args = TQStringList() );

  private:
    static TDEInstance* s_instance;
};

/**
 * This template system has the following features:
 * It allows to create new documents that allready has some contents and a meaningfull
 * document name.
 *
 * Any file can b e used as a template.
 *
 * Special template files can contain macros that are expanded when the document
 * is created, and the cursor can be positioned in the new document.
 *
 * A menu is provided, allowing access to templates located in the KDE file system
 * in the plugins data directory. The menu is dynamically updated.
 *
 * Simple tools are provided for creating/eidting templates.
 *
 * The main class has methods to do all of the work related to use file templates:
 * @li Maintain the Template menu (File, New from Template)
 * @li Load templates
 * @li Provide simple tools for creating/editing templates
*/
class KateFileTemplates : public Kate::Plugin, public Kate::PluginViewInterface
{
  Q_OBJECT
  

  public:
    KateFileTemplates( TQObject* parent = 0, const char* name = 0 );
    virtual ~KateFileTemplates();

    void addView (Kate::MainWindow *win);
    void removeView (Kate::MainWindow *win);

    /**
     * @return a list of unique group names in the template list.
     */
    TQStringList groups();

    /**
     * @return a pointer to the templateinfo collection
     */
    TQPtrList<class TemplateInfo> templates() { return m_templates; }

    /**
     * @return a pointer to the templateInfo for the template at @p index
     * in the template collection.
     */
    class TemplateInfo *templateInfo( int index ) { return m_templates.at( index ); }

    /**
     * @return a a pointer to the active main window
     */
    TQWidget * parentWindow();

  public slots:
    /**
     * Update the template collection by rereading the template
     * directories. Also updates the menu.
     */
    void updateTemplateDirs(const TQString &s=TQString());

  private slots:
    /**
     * Show a file dialog, so that any file can be opened as a template.
     * If the chosen file has the .katetemplate extension, it is parsed,
     * otherwise it is just copied to the new document.
     */
    void slotAny();

    /**
     * Open the template found at @p index in the colletion
     */
    void slotOpenTemplate( int index );

    /**
     * Open the file at @p url as a template. If it has the .katetemplate
     * extension it is parsed, otherwise its content is just copied to the new
     * document.
     */
    void slotOpenTemplate( const KURL &url );

    void slotEditTemplate();

    /**
     * Show a KateTemplateWizard wizard.
     */
    void slotCreateTemplate();

  private:
    void refreshMenu( class PluginView */*class TQPopupMenu **/ );

    TQPtrList<class PluginView> m_views;
    class KActionCollection *m_actionCollection;
    class KRecentFilesAction *m_acRecentTemplates;
    TQPtrList<class TemplateInfo> m_templates;
    class KDirWatch *m_dw;
    class KUser *m_user;
    class KConfig *m_emailstuff;
    class KActionMenu *m_menu;
};

class TemplateInfo;

/**
 * This widget provide a GUI for editing template properties.
 */
class KateTemplateInfoWidget : public TQWidget
{
  Q_OBJECT
  
  public:
    KateTemplateInfoWidget( TQWidget *parent=0, TemplateInfo *info=0, KateFileTemplates *kft=0 );
    ~KateTemplateInfoWidget() {}

    TemplateInfo *info;

    class TQLineEdit *leTemplate, *leDocumentName, *leDescription, *leAuthor;
    class TQComboBox *cmbGroup;
    class TQPushButton *btnHighlight;
    class KIconButton *ibIcon;

  private slots:
    void slotHlSet( int id );

  private:
    KateFileTemplates *kft;
};

/**
  * This wizard helps creating a new template, which is then opened for the user
  * to edit.
  * Basically, the user is offered to select an existing file or template to start
  * from, set template properties, and if a file is loaded, some replacements is
  * done in the text:
  * @li % characters are protected (% -> %%)
  * @li ^ characters are protected (with a backsplash)
  * @li The users name, username and email is replaced by the corresponding macros
  * If so chosen, the file is saved to either the template directory, or a location
  * set by the user.
*/
class KateTemplateWizard : public KWizard
{
  friend class KateFileTemplates;
  Q_OBJECT
  
  public:
    KateTemplateWizard( TQWidget* parent, KateFileTemplates *ktf );
    ~KateTemplateWizard() {}

  public slots:
    void accept();

  private slots:
    void slotTmplateSet( int );
    void slotStateChanged();
    void slotStateChanged( int ) { slotStateChanged(); }
    void slotStateChanged( const TQString& ) { slotStateChanged(); }

  private:
    KateFileTemplates *kft;
    KateTemplateInfoWidget *kti;

    // origin page
    class TQButtonGroup *bgOrigin;
    class KURLRequester *urOrigin;
    class TQPushButton *btnTmpl;
    uint selectedTemplateIdx;

    // location page
    class TQButtonGroup *bgLocation;
    class KURLRequester *urLocation;
    class TQLineEdit *leTemplateFileName;

    // macro replacement page
    class TQCheckBox *cbRRealname, *cbRUsername, *cbREmail;
    TQString sFullname, sEmail/*, sUsername*/;

    // final
    class TQCheckBox *cbOpenTemplate;
};

class KateTemplateManager : public TQWidget
{
  Q_OBJECT
  
  public:
    KateTemplateManager( KateFileTemplates *kft=0, TQWidget *parent=0, const char *name=0 );
    ~KateTemplateManager() {}

  public slots:
    void apply();
    void reload();
    void reset() { reload(); }

  private slots:
    void slotUpload();
    void slotDownload();
    void slotUpdateState();
    void slotEditTemplate();
    void slotRemoveTemplate();

  private:
    class KListView *lvTemplates;
    class TQPushButton *btnNew, *btnEdit, *btnRemove, *btnDownload, *btnUpload;
    KateFileTemplates *kft;
    TQPtrList<class TemplateInfo> *remove;

};

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