kivdirectoryoverlay.cc

00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 2002 Simon MacMullen
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qdict.h>
00021 #include <qpixmap.h>
00022 #include <qpainter.h>
00023 #include <qbitmap.h>
00024 #include <qimage.h>
00025 
00026 #include <kfileivi.h>
00027 #include <kfileitem.h>
00028 #include <kapplication.h>
00029 #include <kdirlister.h>
00030 #include <kstandarddirs.h>
00031 #include <kiconloader.h>
00032 #include <konq_settings.h>
00033 #include <klocale.h>
00034 #include <kdebug.h>
00035 
00036 #include "kivdirectoryoverlay.h"
00037 
00038 KIVDirectoryOverlay::KIVDirectoryOverlay(KFileIVI* directory)
00039 : m_lister(0), m_foundItems(false),
00040   m_containsFolder(false), m_popularIcons(0)
00041 {
00042     if (!m_lister)
00043     {
00044         m_lister = new KDirLister;
00045         m_lister->setAutoErrorHandlingEnabled(false, 0);
00046         connect(m_lister, SIGNAL(completed()), SLOT(slotCompleted()));
00047         connect(m_lister, SIGNAL(newItems( const KFileItemList& )), SLOT(slotNewItems( const KFileItemList& )));
00048         m_lister->setShowingDotFiles(false);
00049     }
00050     m_directory = directory;
00051 }
00052 
00053 KIVDirectoryOverlay::~KIVDirectoryOverlay()
00054 {
00055     if (m_lister) m_lister->stop();
00056     delete m_lister;
00057     delete m_popularIcons;
00058 }
00059 
00060 void KIVDirectoryOverlay::start()
00061 {
00062     if ( m_directory->item()->isReadable() ) {
00063         m_popularIcons = new QDict<int>;
00064         m_popularIcons->setAutoDelete(true);
00065         m_lister->openURL(m_directory->item()->url());
00066     } else {
00067         emit finished();
00068     }
00069 }
00070 
00071 void KIVDirectoryOverlay::timerEvent(QTimerEvent *)
00072 {
00073     m_lister->stop();
00074 }
00075 
00076 void KIVDirectoryOverlay::slotCompleted()
00077 {
00078     if (!m_popularIcons) return;
00079 
00080     // Look through the histogram for the most popular mimetype
00081     QDictIterator<int> currentIcon( (*m_popularIcons) );
00082     unsigned int best = 0;
00083     unsigned int total = 0;
00084     for ( ; currentIcon.current(); ++currentIcon ) {
00085         unsigned int currentCount = (*currentIcon.current());
00086         total += currentCount;
00087         if ( best < currentCount ) {
00088             best = currentCount;
00089             m_bestIcon = currentIcon.currentKey();
00090         }
00091     }
00092 
00093     // Only show folder if there's no other candidate. Most folders contain
00094     // folders. We know this.
00095     if ( m_bestIcon.isNull() && m_containsFolder ) {
00096         m_bestIcon = "folder";
00097     }
00098     
00099     if ( best * 2 < total ) {
00100         m_bestIcon = "kmultiple";
00101     }
00102 
00103     if (!m_bestIcon.isNull()) {
00104         m_directory->setOverlay(m_bestIcon);
00105     }
00106 
00107     delete m_popularIcons;
00108     m_popularIcons = 0;
00109 
00110     emit finished();
00111 }
00112 
00113 void KIVDirectoryOverlay::slotNewItems( const KFileItemList& items )
00114 {
00115     if ( !m_popularIcons) return;
00116 
00117     KFileItemListIterator files( items );
00118 
00119     KFileItem* file;
00120     for ( ; (file = files.current()) != 0; ++files ) {
00121         if ( file -> isFile() ) {
00122 
00123         QString iconName = file -> iconName();
00124         if (!iconName) continue;
00125 
00126         int* iconCount = m_popularIcons -> find( file -> iconName() );
00127         if (!iconCount) {
00128             iconCount = new int(0);
00129             Q_ASSERT(file);
00130             m_popularIcons -> insert(file -> iconName(), iconCount);
00131         }
00132         (*iconCount)++;
00133         } else if ( file -> isDir() ) {
00134             m_containsFolder = true;
00135         }
00136     }
00137 
00138     m_foundItems = true;
00139 }
00140 
00141 #include "kivdirectoryoverlay.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys