summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sidebar/sq_categoriesview.cpp
blob: 6813f6bfe171b40b322c35841cd5149cf4fb3620 (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
/***************************************************************************
                          sq_categoriesview.cpp  -  description
                             -------------------
    begin                : ??? June 3 2006
    copyright            : (C) 2006 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqfile.h>
#include <tqheader.h>

#include <kio/job.h>
#include <ktoolbar.h>
#include <kicontheme.h>
#include <klocale.h>
#include <kmimetype.h>
#include <kfileitem.h>
#include <kurldrag.h>
#include <kinputdialog.h>
#include <kpropertiesdialog.h>
#include <kfiletreeviewitem.h>
#include <kmessagebox.h>

#include "ksquirrel.h"
#include "sq_dir.h"
#include "sq_iconloader.h"
#include "sq_categoriesview.h"
#include "sq_libraryhandler.h"
#include "sq_externaltool.h"
#include "sq_widgetstack.h"
#include "sq_categorybrowsermenu.h"
#include "sq_storagefile.h"
#include "sq_widgetstack.h"
#include "sq_diroperator.h"
#include "sq_treeviewmenu.h"

SQ_CategoriesBox * SQ_CategoriesBox::sing = 0;

/* *************************************************************************************** */

SQ_CategoriesViewBranch::SQ_CategoriesViewBranch(KFileTreeView *tqparent, const KURL &url, const TQString &name, const TQPixmap &pix)
    : KFileTreeBranch(tqparent, url, name, pix)
{}

SQ_CategoriesViewBranch::~SQ_CategoriesViewBranch()
{}

KFileTreeViewItem* SQ_CategoriesViewBranch::createTreeViewItem(KFileTreeViewItem *tqparent, KFileItem *fileItem)
{
    KFileTreeViewItem *i = KFileTreeBranch::createTreeViewItem(tqparent, fileItem);

    /*
     *  In storage there are files with MD5 sum appended to their names.
     *  We should cut off MD5.
     */
    if(i)
    {
        TQString n = i->fileItem()->name();
        int ind = n.findRev('.');

        // OOPS
        if(ind != -1)
            n.truncate(ind);

        i->setText(0, n);
    }

    return i;
}

/* *************************************************************************************** */

SQ_CategoriesView::SQ_CategoriesView(TQWidget *tqparent, const char *name) : KFileTreeView(tqparent, name)
{
    setAcceptDrops(true);

    m_dir = new SQ_Dir(SQ_Dir::Categories);

    // create custom branch
    root = new SQ_CategoriesViewBranch(this, m_dir->root(), i18n("Categories"),
                SQ_IconLoader::instance()->loadIcon("bookmark", KIcon::Desktop, KIcon::SizeSmall));

    addBranch(root);

    header()->hide();
    addColumn(i18n("File"));
    setDirOnlyMode(root, false);
    setRootIsDecorated(true);
    setCurrentItem(root->root());
    root->setChildRecurse(true);
    root->setOpen(true);

    menu = new SQ_TreeViewMenu(this);

    connect(TQT_TQOBJECT(this), TQT_SIGNAL(spacePressed(TQListViewItem*)), TQT_TQOBJECT(this), TQT_SIGNAL(executed(TQListViewItem*)));
    connect(TQT_TQOBJECT(this), TQT_SIGNAL(returnPressed(TQListViewItem*)), TQT_TQOBJECT(this), TQT_SIGNAL(executed(TQListViewItem*)));
    connect(TQT_TQOBJECT(this), TQT_SIGNAL(executed(TQListViewItem*)), TQT_TQOBJECT(this), TQT_SLOT(slotItemExecuted(TQListViewItem*)));
    connect(TQT_TQOBJECT(this), TQT_SIGNAL(contextMenu(KListView*, TQListViewItem*, const TQPoint&)), TQT_TQOBJECT(this), TQT_SLOT(slotContextMenu(KListView*, TQListViewItem*, const TQPoint&)));
}

SQ_CategoriesView::~SQ_CategoriesView()
{
    delete m_dir;
}

void SQ_CategoriesView::slotContextMenu(KListView *, TQListViewItem *item, const TQPoint &p)
{
    if(item)
    {
        KFileTreeViewItem *kfi = static_cast<KFileTreeViewItem*>(item);
        menu->updateDirActions(kfi->isDir(), (item == root->root()));
        menu->setURL(kfi->url());
        menu->exec(p);
    }
}

void SQ_CategoriesView::slotItemExecuted(TQListViewItem *item)
{
    if(!item) return;

    if(item == root->root())
    {
        root->setOpen(true);
        return;
    }

    KFileTreeViewItem *cur = static_cast<KFileTreeViewItem *>(item);

    // file item
    if(cur && !cur->isDir())
    {
        KURL inpath = SQ_StorageFile::readStorageFile(cur->path());

        KFileItem fi(KFileItem::Unknown, KFileItem::Unknown, inpath);
        SQ_WidgetStack::instance()->diroperator()->execute(&fi);
    }
}

/* ************************************************************** */

SQ_CategoriesBox::SQ_CategoriesBox(TQWidget *tqparent, const char *name) : TQVBox(tqparent, name)
{
    sing = this;

    lastdir = i18n("New Category");

    view = new SQ_CategoriesView(this);
    toolbar = new KToolBar(this);

    connect(view, TQT_SIGNAL(dropped(TQDropEvent*, TQListViewItem*, TQListViewItem*)), TQT_TQOBJECT(this), TQT_SLOT(slotDropped(TQDropEvent*, TQListViewItem*, TQListViewItem*)));

    menu = new SQ_CategoryBrowserMenu(view->dir()->root(), 0, "Categories menu");

    toolbar->setIconSize(KIcon::SizeSmall);

    toolbar->insertButton("folder_new", 0, TQT_SIGNAL(clicked()), TQT_TQOBJECT(this), TQT_SLOT(slotNewCategory()), true, i18n("New category"));
    toolbar->insertButton("edittrash", 0, TQT_SIGNAL(clicked()), TQT_TQOBJECT(this), TQT_SLOT(slotDeleteItem()), true, i18n("Delete"));
    toolbar->insertButton("info", 0, TQT_SIGNAL(clicked()), TQT_TQOBJECT(this), TQT_SLOT(slotItemProperties()), true, i18n("Properties"));
    toolbar->insertButton("bookmark_add", 0, TQT_SIGNAL(clicked()), TQT_TQOBJECT(this), TQT_SLOT(slotDefaultCategories()), true, i18n("Create default categories"));

    view->popupMenu()->reconnect(SQ_TreeViewMenu::New, TQT_TQOBJECT(this), TQT_SLOT(slotNewCategory()));
    view->popupMenu()->reconnect(SQ_TreeViewMenu::Delete, TQT_TQOBJECT(this), TQT_SLOT(slotDeleteItem()));
    view->popupMenu()->reconnect(SQ_TreeViewMenu::Properties, TQT_TQOBJECT(this), TQT_SLOT(slotItemProperties()));
}

SQ_CategoriesBox::~SQ_CategoriesBox()
{}

void SQ_CategoriesBox::addToCategory(const TQString &path)
{
    KFileItemList *selected = const_cast<KFileItemList *>(SQ_WidgetStack::instance()->selectedItems());

    if(!selected) return;

    KFileItem *item;

    item = selected->first();

    while(item)
    {
        if(item->isFile())
            SQ_StorageFile::writeStorageFile(path + TQDir::separator() + item->name(), item->url().path());

        item = selected->next();
    }
}

void SQ_CategoriesBox::slotDefaultCategories()
{
    if(KMessageBox::questionYesNo(KSquirrel::app(),
        i18n("This will create default categories: Concerts, Pets, Home, Friends, Free time, Travelling and Nature. Continue?"),
        i18n("Create default categories")) == KMessageBox::Yes)
    {
        TQStringList list;

        list << "Concerts" << "Pets" << "Home" << "Friends" << "Free time" << "Traveling" << "Nature";

        for(TQStringList::iterator it = list.begin();it != list.end();++it)
            view->dir()->mkdir(*it);
    }
}

void SQ_CategoriesBox::slotNewCategory()
{
    bool ok;

    KFileTreeViewItem *cur = view->currentKFileTreeViewItem();

    if(!cur) return;

    if(!cur->isDir())
        cur = static_cast<KFileTreeViewItem *>(cur->tqparent());

    if(!cur) return;

    TQString tmp = KInputDialog::getText(i18n("New Category"), i18n("Create new category:"),
                                         lastdir, &ok, this);
    if(ok)
    {
        lastdir = tmp;
        KIO::mkdir(cur->path() + TQDir::separator() + lastdir);
    }
}

void SQ_CategoriesBox::slotDropped(TQDropEvent *e, TQListViewItem *tqparent, TQListViewItem *item)
{
    if(!item) item = tqparent;

    KFileTreeViewItem *cur = static_cast<KFileTreeViewItem *>(item);

    if(!cur) return;

    KURL::List list;
    KURLDrag::decode(e, list);
    TQString path = cur->path();

    if(list.first().path().startsWith(view->dir()->root()))
        KIO::move(list, cur->url());
    else
    {
        KURL::List::iterator itEnd = list.end();
        TQString mimeDet;

        for(KURL::List::iterator it = list.begin(); it != itEnd;++it)
        {
            mimeDet = KMimeType::findByURL(*it)->name();

            if(mimeDet != "inode/directory")
                SQ_StorageFile::writeStorageFile(path + TQDir::separator() + (*it).fileName(), (*it));
        }
    }
}

void SQ_CategoriesBox::slotDeleteItem()
{
    KFileTreeViewItem *cur = view->currentKFileTreeViewItem();

    if(!cur) return;

    KURL root;
    root.setPath(view->dir()->root());

    if(cur->url().equals(root, true))
        return;

    TQListViewItem *next = cur->itemBelow();
    if(!next) next = cur->itemAbove();

    if(next)
    {
        view->setCurrentItem(next);
        view->setSelected(next, true);
    }

    TQString tmp = cur->path();

    // remove this item manually
    delete cur;
    cur = 0;

    // physically remove file from storage
    KIO::del(tmp, false, false);
}

void SQ_CategoriesBox::slotItemProperties()
{
    KFileTreeViewItem *cur = view->currentKFileTreeViewItem();

    if(!cur) return;

    // directory - just show its properties
    if(cur->isDir())
        (void)new KPropertiesDialog(cur->url(), KSquirrel::app());

    // link to real file
    else
    {
        KURL inpath = SQ_StorageFile::readStorageFile(cur->path());

        if(!inpath.isEmpty())
            (void)new KPropertiesDialog(inpath, KSquirrel::app());
    }
}

#include "sq_categoriesview.moc"