summaryrefslogtreecommitdiffstats
path: root/src/profileengine/editor/profileeditor.cpp
blob: 7732ea904e7f3892a6563e4636b02296c0396957 (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
/***************************************************************************
 *   Copyright (C) 2004 by Alexander Dymo <adymo@tdevelop.org>             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library 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 Library 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.             *
 ***************************************************************************/
#include "profileeditor.h"

#include <tqlayout.h>
#include <klineedit.h>
#include <tqtextedit.h>
#include <tqpalette.h>

#include <kdebug.h>
#include <kpushbutton.h>
#include <klistbox.h>
#include <klistview.h>
#include <kcombobox.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdialogbase.h>
#include <kglobalsettings.h>
#include <tdeversion.h>

#include <profile.h>

#include "addprofilewidget.h"

class ProfileItem: public KListViewItem {
public:
    ProfileItem(KListView *parent, Profile *profile)
        :KListViewItem(parent), m_profile(profile)
    {
        setText(0, profile->genericName());
        setText(1, profile->description());
    }
    
    ProfileItem(KListViewItem *parent, Profile *profile)
        : KListViewItem(parent), m_profile(profile)
    {
        setText(0, profile->genericName());
        setText(1, profile->description());
    }
    
    Profile *profile() const { return m_profile; }
    
private:
    Profile *m_profile;
};

class EDListItem: public KListViewItem{
public:
    EDListItem(KListView *parent, const TQString &text, bool derived)
        : KListViewItem(parent, text), m_derived(derived)
    {
    }
    
    bool isDerived() const { return m_derived; }

    virtual void paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int alignment)
    {
        TQColorGroup cgNew = cg;
        if (m_derived)
            cgNew.setColor(TQColorGroup::Text, TDEGlobalSettings::inactiveTextColor());
        KListViewItem::paintCell(p, cgNew, column, width, alignment);
    }
    
private:
    bool m_derived;
};


class ProfileListBuilding {
public:
    ProfileItem * operator() (ProfileItem *parent, Profile *profile)
    {
        parent->setOpen(true);
        return new ProfileItem(parent, profile);
    }
};


ProfileEditor::ProfileEditor(TQWidget *parent, const char *name)
    :ProfileEditorBase(parent, name)
{
    refresh();
}

void ProfileEditor::refresh()
{
    profilesList->clear();
    
    ProfileItem *item = new ProfileItem(profilesList, engine.rootProfile());
    ProfileListBuilding op;
    engine.walkProfiles<ProfileListBuilding, ProfileItem>(op, item, engine.rootProfile());
    
    profilesList->setSelected(item, true);
    profilesList->setCurrentItem(item);
    
    refreshAvailableList();
    refreshPropertyCombo();
}

void ProfileEditor::refreshPropertyCombo()
{
    TDETrader::OfferList list = TDETrader::self()->query(TQString::fromLatin1("TDevelop/Plugin"));
    TQStringList props;
    for (TDETrader::OfferList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it)
    {
        TQStringList currProps = (*it)->property("X-TDevelop-Properties").toStringList();
        for (TQStringList::const_iterator p = currProps.constBegin(); 
                p != currProps.constEnd(); ++p)
            if (!props.contains(*p))
                props.append(*p);
    }
    props.sort();
    propertyCombo->insertStringList(props);
    propertyCombo->setCurrentText("");
}

void ProfileEditor::refreshAvailableList()
{
    //filling a list of available plugins
    allList->clear();
    allCore = new KListViewItem(allList, i18n("Core"));
    allCore->setOpen(true);
    allGlobal = new KListViewItem(allList, i18n("Global"));
    allGlobal->setOpen(true);
    allProject = new KListViewItem(allList, i18n("Project"));
    allProject->setOpen(true);
    
    TDETrader::OfferList olist = engine.allOffers(ProfileEngine::Core);
    for (TDETrader::OfferList::iterator it = olist.begin(); it != olist.end(); ++it)
        new KListViewItem(allCore, (*it)->desktopEntryName(), (*it)->genericName());
    olist = engine.allOffers(ProfileEngine::Global);
    for (TDETrader::OfferList::iterator it = olist.begin(); it != olist.end(); ++it)
        new KListViewItem(allGlobal, (*it)->desktopEntryName(), (*it)->genericName());
    olist = engine.allOffers(ProfileEngine::Project);
    for (TDETrader::OfferList::iterator it = olist.begin(); it != olist.end(); ++it)
        new KListViewItem(allProject, (*it)->desktopEntryName(), (*it)->genericName());
}

void ProfileEditor::profileExecuted(TQListViewItem *item)
{    
    if (!item || item->text(0) == "KDevelop")
        removeProfileButton->setEnabled(false);
    else
        removeProfileButton->setEnabled(true);

    fillPropertyList(currentProfile());
    fillEDLists(currentProfile());
    fillPluginsList(currentProfile());
}

void ProfileEditor::fillPropertyList(Profile *profile)
{
    derivedPropertiesBox->clear();
    ownPropertiesBox->clear();
    
    Profile::EntryList list = profile->list(Profile::Properties);
    for (Profile::EntryList::const_iterator it = list.begin(); it != list.end(); ++it)
    {
        if ((*it).derived)
            derivedPropertiesBox->insertItem((*it).name);
        else
            ownPropertiesBox->insertItem((*it).name);
    }
}

void ProfileEditor::fillEDLists(Profile *profile)
{    
    //filling a list of enabled plugins
    enabledList->clear();
    Profile::EntryList list = profile->list(Profile::ExplicitEnable);
    for (Profile::EntryList::const_iterator it = list.begin(); it != list.end(); ++it)
        new EDListItem(enabledList, (*it).name, (*it).derived);

    //filling a list of disabled plugins
    disabledList->clear();
    list = profile->list(Profile::ExplicitDisable);
    for (Profile::EntryList::const_iterator it = list.begin(); it != list.end(); ++it)
        new EDListItem(disabledList, (*it).name, (*it).derived);
}

void ProfileEditor::fillPluginsList(Profile *profile)
{
    pluginsView->clear();
    
    KListViewItem *core = new KListViewItem(pluginsView, i18n("Core Plugins"));
    core->setOpen(true);
    KListViewItem *global = new KListViewItem(pluginsView, i18n("Global Plugins"));
    global->setOpen(true);
    KListViewItem *project = new KListViewItem(pluginsView, i18n("Project Plugins"));
    project->setOpen(true);

    TDETrader::OfferList coreOffers = engine.offers(profile->name(), ProfileEngine::Core);
    for (TDETrader::OfferList::const_iterator it = coreOffers.constBegin();
            it != coreOffers.constEnd(); ++it)
        new KListViewItem(core, (*it)->desktopEntryName(), (*it)->genericName(),
            (*it)->property("X-TDevelop-Properties").toStringList().join(", "));
        
    TDETrader::OfferList globalOffers = engine.offers(profile->name(), ProfileEngine::Global);
    for (TDETrader::OfferList::const_iterator it = globalOffers.constBegin();
            it != globalOffers.constEnd(); ++it)
        new KListViewItem(global, (*it)->desktopEntryName(), (*it)->genericName(),
            (*it)->property("X-TDevelop-Properties").toStringList().join(", "));
    
    TDETrader::OfferList projectOffers = engine.offers(profile->name(), ProfileEngine::Project);
    for (TDETrader::OfferList::const_iterator it = projectOffers.constBegin();
            it != projectOffers.constEnd(); ++it)
        new KListViewItem(project, (*it)->desktopEntryName(), (*it)->genericName(),
            (*it)->property("X-TDevelop-Properties").toStringList().join(", "));
}

void ProfileEditor::propertyExecuted(TQListBoxItem *item)
{
    removePropertyButton->setEnabled(item != 0);
}

void ProfileEditor::addProfile()
{
    if (!profilesList->currentItem())
        return;
    
    KDialogBase dlg(KDialogBase::Plain, i18n("Add Profile"), KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Ok);
    dlg.plainPage()->setMargin(0);
    (new TQVBoxLayout(dlg.plainPage(), 0, 0))->setAutoAdd(true);
    AddProfileWidget *prof = new AddProfileWidget(dlg.plainPage());
    prof->nameEdit->setFocus();
    if (dlg.exec() == TQDialog::Accepted)
    {
        Profile *profile = new Profile(currentProfile(), prof->nameEdit->text(), 
            prof->genericNameEdit->text(), 
            prof->descriptionEdit->text());
        profilesList->currentItem()->setOpen(true);
        new ProfileItem(static_cast<KListViewItem*>(profilesList->currentItem()), profile);
    }
}

void ProfileEditor::removeProfile()
{
    if (KMessageBox::warningContinueCancel(this, i18n("Remove selected profile and all its subprofiles?"),
        i18n("Remove Profile"),KStdGuiItem::del()) == KMessageBox::Continue)
    {
        Profile *profile = currentProfile();
        if (profile->remove())
        {
            TQListViewItem *item = profilesList->currentItem();
            profilesList->setCurrentItem(item->parent());
            profile->detachFromParent();
            delete profile;
            delete item;
        }
        else
            KMessageBox::error(this, i18n("Cannot remove this profile because it is not a local (user-created) profile."), i18n("Remove Profile"));
    }
}

void ProfileEditor::addProperty()
{
    if ( (!propertyCombo->currentText().isEmpty()) &&
        (ownPropertiesBox->findItem(propertyCombo->currentText()) == 0) &&
        (derivedPropertiesBox->findItem(propertyCombo->currentText()) == 0) )
    {
        ownPropertiesBox->insertItem(propertyCombo->currentText());
        
        currentProfile()->addEntry(Profile::Properties, propertyCombo->currentText());
        currentProfile()->save();
    }
    
    fillPluginsList(currentProfile());
}

void ProfileEditor::removeProperty()
{
    currentProfile()->removeEntry(Profile::Properties, ownPropertiesBox->currentText());
    currentProfile()->save();
    
    ownPropertiesBox->removeItem(ownPropertiesBox->currentItem());

    fillPluginsList(currentProfile());
}

Profile *ProfileEditor::currentProfile()
{
    ProfileItem *item = dynamic_cast<ProfileItem*>(profilesList->currentItem());
    if (!item)
        return 0;
    return item->profile();
}

void ProfileEditor::accept()
{
}

void ProfileEditor::addEnabled()
{
    if (!allList->currentItem() && allEdit->text().isEmpty())
        return;
    TQString text;
    if (!allEdit->text().isEmpty())
    {
        text = allEdit->text();
        allEdit->clear();
    }
    else
    {
        if ((allList->currentItem() == allGlobal) || (allList->currentItem() == allProject))
            return;
        text = allList->currentItem()->text(0);
    }
    
    if (enabledList->findItem(text, 0) != 0)
        return;
    if (disabledList->findItem(text, 0) != 0)
    {
        KMessageBox::error(this, i18n("This plugin is already contained in the list of disabled plugins."),
            i18n("Enable Plugin"));
        return;
    }
    currentProfile()->addEntry(Profile::ExplicitEnable, text);
    currentProfile()->save();
    fillPluginsList(currentProfile());
    new EDListItem(enabledList, text, false);
}

void ProfileEditor::delEnabled()
{
    if (!enabledList->currentItem())
        return;
    
    EDListItem *item = dynamic_cast<EDListItem*>(enabledList->currentItem());
    if (item && !item->isDerived())
    {
        currentProfile()->removeEntry(Profile::ExplicitEnable, enabledList->currentItem()->text(0));
        currentProfile()->save();
        fillPluginsList(currentProfile());
        delete enabledList->currentItem();
    }
}

void ProfileEditor::addDisabled()
{
    if (!allList->currentItem() && allEdit->text().isEmpty())
        return;
    TQString text;
    if (!allEdit->text().isEmpty())
    {
        text = allEdit->text();
        allEdit->clear();
    }
    else
    {
        if ((allList->currentItem() == allGlobal) || (allList->currentItem() == allProject))
            return;
        text = allList->currentItem()->text(0);
    }
    
    if (disabledList->findItem(text, 0) != 0)
        return;
    if (enabledList->findItem(text, 0) != 0)
    {
        KMessageBox::error(this, i18n("This plugin is already contained in the list of enabled plugins."),
            i18n("Disable Plugin"));
        return;
    }
    currentProfile()->addEntry(Profile::ExplicitDisable, text);
    currentProfile()->save();
    fillPluginsList(currentProfile());
    new EDListItem(disabledList, text, false);
}

void ProfileEditor::delDisabled()
{
    if (!disabledList->currentItem())
        return;
    
    EDListItem *item = dynamic_cast<EDListItem*>(disabledList->currentItem());
    if (item && !item->isDerived())
    {
        currentProfile()->removeEntry(Profile::ExplicitDisable, disabledList->currentItem()->text(0));
        delete disabledList->currentItem();
        currentProfile()->save();
        fillPluginsList(currentProfile());
    }
}

#include "profileeditor.moc"