summaryrefslogtreecommitdiffstats
path: root/konqueror/listview/konq_infolistviewwidget.cc
blob: 6adf36feb625c0d32acc33474745a9b85e875f78 (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
/* This file is part of the KDE project
   Copyright (C) 2002 Rolf Magnus <ramagnus@kde.org>
   Copyright (C) 2003 Waldo Bastian <bastian@kde.org>

   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 version 2.0

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include "konq_infolistviewwidget.h"
#include "konq_infolistviewwidget.moc"
#include "konq_infolistviewitem.h"
#include "konq_listview.h"

#include <klocale.h>
#include <kfilemetainfo.h>
#include <kdebug.h>
#include <kaction.h>
#include <kservicetype.h>
#include <kuserprofile.h>
#include <kio/metainfojob.h>

#include <tqstringlist.h>

KonqInfoListViewWidget::KonqInfoListViewWidget( KonqListView* parent,
                                                TQWidget* parentWidget)
  : KonqBaseListViewWidget(parent, parentWidget)
{
    m_metaInfoJob = 0;

    m_mtSelector = new KSelectAction(i18n("View &As"), 0, TQT_TQOBJECT(this),
                                     TQT_SLOT(slotSelectMimeType()),
                                     parent->actionCollection(), "view_as" );

    kdDebug(1203) << "created info list view\n";
}

KonqInfoListViewWidget::~KonqInfoListViewWidget()
{
    delete m_mtSelector;
    delete m_metaInfoJob;
}

void KonqInfoListViewWidget::slotSelectMimeType()
{
    TQString comment = m_mtSelector->currentText();

    // find the mime type by comment
    TQMapIterator<TQString, KonqILVMimeType> it;
    for (it = m_counts.begin(); it!=m_counts.end(); ++it)
    {
        if ((*it).mimetype->comment() == comment)
        {
            m_favorite = *it;
            createFavoriteColumns();
            rebuildView();
            break;
        }
    }

}

void KonqInfoListViewWidget::createColumns()
{
   if (columns()<1)
   {
        // we can only create the minimum columns here, because we don't get the
        // items, from which we determine the columns yet.
        addColumn(i18n("Filename"), m_filenameColumnWidth);
   }
}

void KonqInfoListViewWidget::createFavoriteColumns()
{
    while (columns()>1)
    {
        kdDebug(1203) << "removing column " << columnText(columns()-1) << endl;
        removeColumn(columns()-1);
    }

    // we need to get the preferred keys of the favorite
    const KFileMimeTypeInfo* mimeTypeInfo;

    if (m_favorite.mimetype &&
          (mimeTypeInfo = KFileMetaInfoProvider::self()
                            ->mimeTypeInfo(m_favorite.mimetype->name())))
    {
        TQStringList preferredCols = mimeTypeInfo->preferredKeys();
        m_columnKeys.clear(); //We create the columnKeys as we're creating
        //the actual columns, to make sure they're synced

        // get the translations
        TQStringList groups = mimeTypeInfo->preferredGroups();
        if (groups.isEmpty()) groups = mimeTypeInfo->supportedGroups();

        TQStringList::Iterator prefKey = preferredCols.begin();
        for (; prefKey != preferredCols.end(); ++prefKey)
        {
            TQStringList::Iterator group = groups.begin();
            for (; group != groups.end(); ++group)
            {
                const KFileMimeTypeInfo::GroupInfo* groupInfo =
                                    mimeTypeInfo->groupInfo(*group);
                if(groupInfo)
                {
                    TQStringList keys = groupInfo->supportedKeys();
                    TQStringList::Iterator key = keys.begin();
                    for (; key != keys.end(); ++key)
                    {
                        if ( *key == *prefKey )
                        {
                            const KFileMimeTypeInfo::ItemInfo* itemInfo =
                                groupInfo->itemInfo(*key);
                            addColumn(itemInfo->translatedKey());
                            m_columnKeys.append(*key);
                        }
                    }
                }
            }
        }
    }
    else
    {
        KonqBaseListViewWidget::createColumns();
    }

}

bool KonqInfoListViewWidget::openURL( const KURL &url )
{
    bool ret = KonqBaseListViewWidget::openURL(url);
    m_bTopLevelComplete = true;
    return ret;
}

void KonqInfoListViewWidget::rebuildView()
{
    // make a KFileItemList from all our Items
    KFileItemList list;

    TQListViewItemIterator it(this);
    for (; it.current(); ++it)
    {
        list.append(static_cast<KonqInfoListViewItem*>(it.current())->item());
    }

    // now we can remove all the listview items
    clear();

    // and rebuild them
    for (TQPtrListIterator<KFileItem> kit(list); kit.current(); ++kit)
    {
        KonqInfoListViewItem* tmp = new KonqInfoListViewItem( this, *kit );
//        if (m_goToFirstItem==false)
//            if (m_itemFound==false)
//                if (tmp->text(0)==m_itemToGoTo)
//                {
//kdDebug() << "Line " << __LINE__ << endl;
//                    setCurrentItem(tmp);
//kdDebug() << "Line " << __LINE__ << endl;
//                    ensureItemVisible(tmp);
//kdDebug() << "Line " << __LINE__ << endl;
//                    emit selectionChanged();
                    //selectCurrentItemAndEnableSelectedBySimpleMoveMode();
//                    m_itemFound=true;
//kdDebug() << "Line " << __LINE__ << endl;
//                };

        tmp->gotMetaInfo();
    }


    if ( !viewport()->isUpdatesEnabled() )
    {
        viewport()->tqsetUpdatesEnabled( true );
        tqsetUpdatesEnabled( true );
        triggerUpdate();
    }
}

void KonqInfoListViewWidget::slotNewItems( const KFileItemList& list)
{
    slotStarted(); // ############# WHY?
    for (TQPtrListIterator<KFileItem> kit ( list ); kit.current(); ++kit )
    {
        KonqInfoListViewItem * tmp = new KonqInfoListViewItem( this, *kit );

        if (!m_itemFound && tmp->text(0)==m_itemToGoTo)
        {
            setCurrentItem(tmp);
            m_itemFound=true;
        }

        if ( !m_itemsToSelect.isEmpty() ) {
           TQStringList::Iterator tsit = m_itemsToSelect.find( (*kit)->name() );
           if ( tsit != m_itemsToSelect.end() ) {
              m_itemsToSelect.remove( tsit );
              setSelected( tmp, true );
           }
        }

        if ( !(*kit)->isMimeTypeKnown() )
            m_pBrowserView->lstPendingMimeIconItems().append( tmp );
    }
    m_pBrowserView->newItems( list );

    if ( !viewport()->isUpdatesEnabled() )
    {
        viewport()->tqsetUpdatesEnabled( true );
        tqsetUpdatesEnabled( true );
        triggerUpdate();
    }

    slotUpdateBackground();

    if ( !m_favorite.mimetype )
        determineCounts(list);

    kdDebug(1203) << " ------------------------ startin metainfo job ------\n";

    // start getting metainfo from the files
    if (m_metaInfoJob)
    {
       for (TQPtrListIterator<KFileItem> kit ( list ); kit.current(); ++kit )
          m_metaInfoTodo.append(kit.current());
    }
    else
    {
        m_metaInfoJob = KIO::fileMetaInfo(list);
        connect( m_metaInfoJob, TQT_SIGNAL( gotMetaInfo( const KFileItem*)),
             this, TQT_SLOT( slotMetaInfo( const KFileItem*)));
        connect( m_metaInfoJob, TQT_SIGNAL( result( KIO::Job*)),
             this, TQT_SLOT( slotMetaInfoResult()));
    }
}

void KonqInfoListViewWidget::slotRefreshItems( const KFileItemList& list)
{
    kdDebug(1203) << " ------------------------ startin metainfo job ------\n";

    // start getting metainfo from the files
    if (m_metaInfoJob)
    {
       for (TQPtrListIterator<KFileItem> kit ( list ); kit.current(); ++kit )
          m_metaInfoTodo.append(kit.current());
    }
    else
    {
        m_metaInfoJob = KIO::fileMetaInfo(list);
        connect( m_metaInfoJob, TQT_SIGNAL( gotMetaInfo( const KFileItem*)),
             this, TQT_SLOT( slotMetaInfo( const KFileItem*)));
        connect( m_metaInfoJob, TQT_SIGNAL( result( KIO::Job*)),
             this, TQT_SLOT( slotMetaInfoResult()));
    }
    KonqBaseListViewWidget::slotRefreshItems(list);
}

void KonqInfoListViewWidget::slotDeleteItem( KFileItem * item )
{
    m_metaInfoTodo.removeRef(item);
    if (m_metaInfoJob)
       m_metaInfoJob->removeItem(item);

    KonqBaseListViewWidget::slotDeleteItem(item);
}

void KonqInfoListViewWidget::slotClear()
{
    m_metaInfoTodo.clear();
    delete m_metaInfoJob;
    m_metaInfoJob = 0;
    m_favorite = KonqILVMimeType();

    KonqBaseListViewWidget::slotClear();
}

void KonqInfoListViewWidget::slotMetaInfo(const KFileItem* item)
{
    // need to search for the item (any better way?)
    TQListViewItemIterator it(this);
    for (; it.current(); ++it)
    {
        if (static_cast<KonqInfoListViewItem*>(it.current())->item()==item)
        {
            static_cast<KonqInfoListViewItem*>(it.current())->gotMetaInfo();
            return;
        }
    }

    // we should never reach this place
    Q_ASSERT(false);
}

void KonqInfoListViewWidget::slotMetaInfoResult()
{
    m_metaInfoJob = 0;
    if (m_metaInfoTodo.isEmpty())
    {
       m_bTopLevelComplete = false;
       kdDebug(1203) << "emitting completed!!!!!!!!!!!!!!!!\n";
       slotCompleted();
    }
    else
    {
        m_metaInfoJob = KIO::fileMetaInfo(m_metaInfoTodo);
        connect( m_metaInfoJob, TQT_SIGNAL( gotMetaInfo( const KFileItem*)),
             this, TQT_SLOT( slotMetaInfo( const KFileItem*)));
        connect( m_metaInfoJob, TQT_SIGNAL( result( KIO::Job*)),
             this, TQT_SLOT( slotMetaInfoResult()));
        m_metaInfoTodo.clear();
    }
}

void KonqInfoListViewWidget::determineCounts(const KFileItemList& list)
{
    m_counts.clear();
    m_favorite = KonqILVMimeType();
    // get the counts
    for (KFileItemListIterator it(list); it.current(); ++it)
    {
        TQString mt = it.current()->mimetype();
        m_counts[mt].count++;
        if (!m_counts[mt].mimetype)
            m_counts[mt].mimetype = it.current()->determineMimeType();
    }

    // and look for the plugins
    kdDebug(1203) << "counts are:\n";

    KFileMetaInfoProvider* prov = KFileMetaInfoProvider::self();

    TQStringList mtlist;

    TQMapIterator<TQString, KonqILVMimeType> it;
    for (it = m_counts.begin(); it!=m_counts.end(); ++it)
    {
        // look if there is a plugin for this mimetype
        // and look for the "favorite" mimetype
#ifdef _GNUC
#warning ### change this
#endif
        // this will load the plugin which we don't need because we delegate
        // the job to the kioslave
        (*it).hasPlugin = prov->plugin(it.key());

        if ( (*it).hasPlugin)
        {
            mtlist << (*it).mimetype->comment();
            if (m_favorite.count <= (*it).count)
                m_favorite = *it;
        }

        kdDebug(1203) << it.key() << " -> " << (*it).count
                      << " item(s) / plugin: "
                      << ((*it).hasPlugin ? "yes" : "no ") << endl;
    }

    m_mtSelector->setItems(mtlist);
//    TQPopupMenu* menu = m_mtSelector->popupMenu();

    // insert the icons
//    for (int i=0; i<menu->count(); ++i)
//    {
//        menu->changeItem(i, TQIconSet(blah));
//    }

    if (m_favorite.mimetype)
    {
          m_mtSelector->setCurrentItem(mtlist.findIndex(m_favorite.mimetype->comment()));
          kdDebug(1203) << "favorite mimetype is " << m_favorite.mimetype->name() << endl;
    }
    createFavoriteColumns();
}