summaryrefslogtreecommitdiffstats
path: root/kicker/applets/media/mediaapplet.cpp
blob: af1113f1b6ec19e6ce10b760cc80e26513e5e5b6 (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
/* This file is part of the KDE project
   Copyright (c) 2004 Kevin Ottens <ervin ipsquad net>

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

#include <kglobal.h>
#include <klocale.h>
#include <tdeconfig.h>
#include <tdeapplication.h>
#include <tdeaboutdata.h>
#include <tdeaboutapplication.h>
#include <kdebug.h>
#include <tdepopupmenu.h>
#include <kiconloader.h>

#include "mediaapplet.h"

#include "preferencesdialog.h"

extern "C"
{
    KDE_EXPORT KPanelApplet* init( TQWidget *parent, const TQString configFile)
    {
        TDEGlobal::locale()->insertCatalogue("mediaapplet");
        return new MediaApplet(configFile, KPanelApplet::Normal,
            KPanelApplet::About | KPanelApplet::Preferences,
            parent, "mediaapplet");
    }
}

MediaApplet::MediaApplet(const TQString& configFile, Type type, int actions, TQWidget *parent, const char *name)
    : KPanelApplet(configFile, type, actions, parent, name),
    mButtonSizeSum(0)
{
    if (!parent)
        setBackgroundMode(X11ParentRelative);
        
    setBackgroundOrigin(AncestorOrigin);
    
    setAcceptDrops(true);
    
    loadConfig();
    
    mpDirLister = new KDirLister();
    
    connect( mpDirLister, TQT_SIGNAL( clear() ),
            this, TQT_SLOT( slotClear() ) );
    connect( mpDirLister, TQT_SIGNAL( started(const KURL&) ),
            this, TQT_SLOT( slotStarted(const KURL&) ) );
    connect( mpDirLister, TQT_SIGNAL( completed() ),
            this, TQT_SLOT( slotCompleted() ) );
    connect( mpDirLister, TQT_SIGNAL( newItems( const KFileItemList & ) ),
            this, TQT_SLOT( slotNewItems( const KFileItemList & ) ) );
    connect( mpDirLister, TQT_SIGNAL( deleteItem( KFileItem * ) ),
            this, TQT_SLOT( slotDeleteItem( KFileItem * ) ) );
    connect( mpDirLister, TQT_SIGNAL( refreshItems( const KFileItemList & ) ),
            this, TQT_SLOT( slotRefreshItems( const KFileItemList & ) ) );
    
    reloadList();
}

MediaApplet::~MediaApplet()
{
    delete mpDirLister;
    
    while (!mButtonList.isEmpty())
    {
        MediumButton *b = mButtonList.first();
        mButtonList.remove(b);
        delete b;
    }
    
    TDEGlobal::locale()->removeCatalogue("mediaapplet");
}

void MediaApplet::about()
{
    TDEAboutData data("mediaapplet",
                    I18N_NOOP("Media Applet"),
                    "1.0",
                    I18N_NOOP("\"media:/\" ioslave frontend applet"),
                    TDEAboutData::License_GPL_V2,
                    "(c) 2004, Kevin Ottens");
    
    data.addAuthor("Kevin \'ervin\' Ottens",
                    I18N_NOOP("Maintainer"),
                    "ervin ipsquad net",
                    "http://ervin.ipsquad.net");
    
    data.addCredit("Joseph Wenninger",
                    I18N_NOOP("Good mentor, patient and helpful. Thanks for all!"),
                    "jowenn@kde.org");
    
    TDEAboutApplication dialog(&data);
    dialog.exec();
}

void MediaApplet::preferences()
{
    PreferencesDialog dialog(mMedia);
    
    dialog.setExcludedMediumTypes(mExcludedTypesList);
    dialog.setExcludedMedia(mExcludedList);
    
    if(dialog.exec())
    {
        mExcludedTypesList = dialog.excludedMediumTypes();
        mExcludedList = dialog.excludedMedia();
        saveConfig();
        reloadList();
    }
}

int MediaApplet::widthForHeight( int /*height*/ ) const
{
    return mButtonSizeSum;
}

int MediaApplet::heightForWidth( int /*width*/ ) const
{
    return mButtonSizeSum;
}

void MediaApplet::resizeEvent( TQResizeEvent *)
{
    arrangeButtons();
}

void MediaApplet::arrangeButtons()
{
    int button_size = 1;
    int x_offset = 0;
    int y_offset = 0;
    
    // Determine upper bound for the button size
    MediumButtonList::iterator it;
    MediumButtonList::iterator end = mButtonList.end();
    for ( it = mButtonList.begin(); it != end; ++it )
    {
        MediumButton *button = *it;
        
        button_size = std::max(button_size,
                            orientation() == Qt::Vertical ?
                    button->heightForWidth(width()) :
                button->widthForHeight(height()) );
    //		                           button->widthForHeight(height()) :
    //		                           button->heightForWidth(width()) );
    }
    
    int kicker_size;
    if (orientation() == Qt::Vertical)
    {
        kicker_size = width();
    }
    else
    {
        kicker_size = height();
    }
    
    unsigned int max_packed_buttons = kicker_size / button_size;
    // Center icons if we only have one column/row
    if (mButtonList.count() < max_packed_buttons)
    {
        max_packed_buttons = TQMAX(uint(1), mButtonList.count());
    }
            max_packed_buttons = TQMAX(uint(1), max_packed_buttons);
    
    int padded_button_size = kicker_size / max_packed_buttons;
    mButtonSizeSum = 0;
    unsigned int pack_count = 0;
    
    // Arrange the buttons. If kicker is more than twice as high/wide
    // as the maximum preferred size of an icon, we put several icons
    // in one column/row
    for ( it = mButtonList.begin(); it != end; ++it )
    {
        MediumButton *button = *it;
    
        button->move(x_offset, y_offset);
        button->setPanelPosition(position());
    
        if(pack_count == 0)
        {
        mButtonSizeSum += button_size;
        }
        
        ++pack_count;
    
        if(orientation() == Qt::Vertical)
                    {
        if (pack_count < max_packed_buttons)
        {
            x_offset += padded_button_size;
        }
        else
        {
            x_offset = 0;
            y_offset += button_size;
            pack_count = 0;
        }
        
        button->resize(padded_button_size, button_size);
        }
        else
        {
        if (pack_count < max_packed_buttons)
        {
            y_offset += padded_button_size;
                            }
        else
        {
            y_offset = 0;
            x_offset += button_size;
            pack_count = 0;
        }
        
        button->resize(button_size, padded_button_size);
        }
        
        button->unsetPalette();
        button->setBackgroundOrigin(AncestorOrigin);
    }
    
    updateGeometry();
    emit updateLayout();
}

void MediaApplet::slotClear()
{
    kdDebug()<<"MediaApplet::slotClear"<<endl;
    
    while (!mButtonList.isEmpty())
    {
        MediumButton *b = mButtonList.first();
        mButtonList.remove(b);
        delete b;
    }
    
    arrangeButtons();
}

void MediaApplet::slotStarted(const KURL &/*url*/)
{
}

void MediaApplet::slotCompleted()
{
    mMedia = mpDirLister->items(KDirLister::AllItems);
}

void MediaApplet::slotNewItems(const KFileItemList &entries)
{
    kdDebug()<<"MediaApplet::slotNewItems"<<endl;
    
    for(KFileItemListIterator it(entries); it.current(); ++it)
    {
        kdDebug() << "item: " << it.current()->url() << endl;
    
        bool found = false;
        MediumButtonList::iterator it2;
        MediumButtonList::iterator end = mButtonList.end();
        for ( it2 = mButtonList.begin(); it2 != end; ++it2 )
        {
        MediumButton *button = *it2;
        
        if(button->fileItem().url()==it.current()->url())
        {
            found = true;
            button->setFileItem(*it.current());
            break;
        }
        }
        
        if(!found && !mExcludedList.contains(it.current()->url().url()) )
        {
        MediumButton *button = new MediumButton(this, *it.current());
        button->show();
        mButtonList.append(button);
        }
    }
    
    arrangeButtons();
}

void MediaApplet::slotDeleteItem(KFileItem *fileItem)
{
    kdDebug()<<"MediumApplet::slotDeleteItem:"<< fileItem->url() << endl;
    
    MediumButtonList::iterator it;
    MediumButtonList::iterator end = mButtonList.end();
    for ( it = mButtonList.begin(); it != end; ++it )
    {
        MediumButton *button = *it;
        
        if(button->fileItem().url()==fileItem->url())
        {
        mButtonList.remove(button);
        delete button;
        break;
        }
    }
    slotCompleted();
    arrangeButtons();
}

void MediaApplet::slotRefreshItems(const KFileItemList &entries)
{
    for(KFileItemListIterator it(entries); it.current(); ++it)
    {
        kdDebug()<<"MediaApplet::slotRefreshItems:"<<(*it.current()).url().url()<<endl;
    
        TQString mimetype = (*it.current()).mimetype();
        bool found = false;
    
        kdDebug()<<"mimetype="<<mimetype<<endl;
    
        MediumButtonList::iterator it2;
        MediumButtonList::iterator end = mButtonList.end();
        for ( it2 = mButtonList.begin(); it2 != end; ++it2 )
        {
        MediumButton *button = *it2;
        
        if(button->fileItem().url()==(*it.current()).url())
        {
            if(mExcludedTypesList.contains(mimetype))
            {
            mButtonList.remove(button);
            delete button;
            }
            else
            {
            button->setFileItem(*it.current());
            }
            found = true;
            break;
        }
        }
    
        if(!found && !mExcludedTypesList.contains(mimetype) && !mExcludedList.contains(it.current()->url().url()) )
        {
        MediumButton *button = new MediumButton(this, *it.current());
        button->show();
        mButtonList.append(button);
        }
    }
    
    arrangeButtons();
}

void MediaApplet::positionChange(Position)
{
    arrangeButtons();
}

void MediaApplet::loadConfig()
{
    TDEConfig *c = config();
    c->setGroup("General");
    
    if(c->hasKey("ExcludedTypes"))
    {
        mExcludedTypesList = c->readListEntry("ExcludedTypes",';');
    }
    else
    {
        mExcludedTypesList.clear();
        mExcludedTypesList << "media/hdd_mounted";
        mExcludedTypesList << "media/hdd_unmounted";
        mExcludedTypesList << "media/nfs_mounted";
        mExcludedTypesList << "media/nfs_unmounted";
        mExcludedTypesList << "media/smb_mounted";
        mExcludedTypesList << "media/smb_unmounted";
    }
    
    if(c->hasKey("ExcludedMedia"))
    {
        mExcludedList = c->readListEntry("ExcludedMedia",';');
    }
    else
    {
        mExcludedList.clear();
    }
}

void MediaApplet::saveConfig()
{
    TDEConfig *c = config();
    c->setGroup("General");
    
    c->writeEntry("ExcludedTypes", mExcludedTypesList, ';');
    c->writeEntry("ExcludedMedia", mExcludedList, ';');
    
    c->sync();
}

void MediaApplet::reloadList()
{
    mpDirLister->stop();
    
    while (!mButtonList.isEmpty())
    {
        MediumButton *b = mButtonList.first();
        mButtonList.remove(b);
        delete b;
    }
    
    mpDirLister->clearMimeFilter();
    mpDirLister->setMimeExcludeFilter(mExcludedTypesList);
    mpDirLister->openURL(KURL("media:/"));
}

void MediaApplet::mousePressEvent(TQMouseEvent *e)
{
    if(e->button()==Qt::RightButton)
    {
        TDEPopupMenu menu(this);
    
        menu.insertTitle(i18n("Media"));
        menu.insertItem(SmallIcon("configure"), i18n("&Configure..."), 1);
    
        int choice = menu.exec(this->mapToGlobal(e->pos()));
    
        if(choice==1)
        {
            preferences();
        }
    }
}

#include "mediaapplet.moc"