~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to libkonq/kivdirectoryoverlay.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-27 12:09:48 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527120948-dottsyd5rcwhzd36
Tags: 4:4.0.80-1ubuntu1
* Merge with Debian
 - remove 97_fix_target_link_libraries.diff
 - Add replaces/conflicts on -kde4 packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the KDE libraries
2
 
    Copyright (C) 2002 Simon MacMullen
3
 
 
4
 
    This library is free software; you can redistribute it and/or
5
 
    modify it under the terms of the GNU Library General Public
6
 
    License as published by the Free Software Foundation; either
7
 
    version 2 of the License, or (at your option) any later version.
8
 
 
9
 
    This library is distributed in the hope that it will be useful,
10
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
    Library General Public License for more details.
13
 
 
14
 
    You should have received a copy of the GNU Library General Public License
15
 
    along with this library; see the file COPYING.LIB.  If not, write to
16
 
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
 
    Boston, MA 02110-1301, USA.
18
 
*/
19
 
 
20
 
#include <qdict.h>
21
 
#include <qpixmap.h>
22
 
#include <qpainter.h>
23
 
#include <qbitmap.h>
24
 
#include <qimage.h>
25
 
 
26
 
#include <kfileivi.h>
27
 
#include <kfileitem.h>
28
 
#include <kapplication.h>
29
 
#include <kdirlister.h>
30
 
#include <kstandarddirs.h>
31
 
#include <kiconloader.h>
32
 
#include <konq_settings.h>
33
 
#include <klocale.h>
34
 
#include <kdebug.h>
35
 
 
36
 
#include "kivdirectoryoverlay.h"
37
 
 
38
 
KIVDirectoryOverlay::KIVDirectoryOverlay(KFileIVI* directory)
39
 
: m_lister(0), m_foundItems(false),
40
 
  m_containsFolder(false), m_popularIcons(0)
41
 
{
42
 
    if (!m_lister)
43
 
    {
44
 
        m_lister = new KDirLister;
45
 
        m_lister->setAutoErrorHandlingEnabled(false, 0);
46
 
        connect(m_lister, SIGNAL(completed()), SLOT(slotCompleted()));
47
 
        connect(m_lister, SIGNAL(newItems( const KFileItemList& )), SLOT(slotNewItems( const KFileItemList& )));
48
 
        m_lister->setShowingDotFiles(false);
49
 
    }
50
 
    m_directory = directory;
51
 
}
52
 
 
53
 
KIVDirectoryOverlay::~KIVDirectoryOverlay()
54
 
{
55
 
    if (m_lister) m_lister->stop();
56
 
    delete m_lister;
57
 
    delete m_popularIcons;
58
 
}
59
 
 
60
 
void KIVDirectoryOverlay::start()
61
 
{
62
 
    if ( m_directory->item()->isReadable() ) {
63
 
        m_popularIcons = new QDict<int>;
64
 
        m_popularIcons->setAutoDelete(true);
65
 
        m_lister->openURL(m_directory->item()->url());
66
 
    } else {
67
 
        emit finished();
68
 
    }
69
 
}
70
 
 
71
 
void KIVDirectoryOverlay::timerEvent(QTimerEvent *)
72
 
{
73
 
    m_lister->stop();
74
 
}
75
 
 
76
 
void KIVDirectoryOverlay::slotCompleted()
77
 
{
78
 
    if (!m_popularIcons) return;
79
 
 
80
 
    // Look through the histogram for the most popular mimetype
81
 
    QDictIterator<int> currentIcon( (*m_popularIcons) );
82
 
    unsigned int best = 0;
83
 
    unsigned int total = 0;
84
 
    for ( ; currentIcon.current(); ++currentIcon ) {
85
 
        unsigned int currentCount = (*currentIcon.current());
86
 
        total += currentCount;
87
 
        if ( best < currentCount ) {
88
 
            best = currentCount;
89
 
            m_bestIcon = currentIcon.currentKey();
90
 
        }
91
 
    }
92
 
 
93
 
    // Only show folder if there's no other candidate. Most folders contain
94
 
    // folders. We know this.
95
 
    if ( m_bestIcon.isNull() && m_containsFolder ) {
96
 
        m_bestIcon = "folder";
97
 
    }
98
 
    
99
 
    if ( best * 2 < total ) {
100
 
        m_bestIcon = "kmultiple";
101
 
    }
102
 
 
103
 
    if (!m_bestIcon.isNull()) {
104
 
        m_directory->setOverlay(m_bestIcon);
105
 
    }
106
 
 
107
 
    delete m_popularIcons;
108
 
    m_popularIcons = 0;
109
 
 
110
 
    emit finished();
111
 
}
112
 
 
113
 
void KIVDirectoryOverlay::slotNewItems( const KFileItemList& items )
114
 
{
115
 
    if ( !m_popularIcons) return;
116
 
 
117
 
    KFileItemListIterator files( items );
118
 
 
119
 
    KFileItem* file;
120
 
    for ( ; (file = files.current()) != 0; ++files ) {
121
 
        if ( file -> isFile() ) {
122
 
 
123
 
        QString iconName = file -> iconName();
124
 
        if (!iconName) continue;
125
 
 
126
 
        int* iconCount = m_popularIcons -> find( file -> iconName() );
127
 
        if (!iconCount) {
128
 
            iconCount = new int(0);
129
 
            Q_ASSERT(file);
130
 
            m_popularIcons -> insert(file -> iconName(), iconCount);
131
 
        }
132
 
        (*iconCount)++;
133
 
        } else if ( file -> isDir() ) {
134
 
            m_containsFolder = true;
135
 
        }
136
 
    }
137
 
 
138
 
    m_foundItems = true;
139
 
}
140
 
 
141
 
#include "kivdirectoryoverlay.moc"