libkonq Library API Documentation

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., 59 Temple Place - Suite 330, 00017 Boston, MA 02111-1307, USA. 00018 */ 00019 00020 // $Id: kivdirectoryoverlay.cc,v 1.6 2002/06/20 10:27:49 macmullen Exp $ 00021 00022 #include <qdict.h> 00023 #include <qpixmap.h> 00024 #include <qpainter.h> 00025 #include <qbitmap.h> 00026 #include <qimage.h> 00027 00028 #include <kfileivi.h> 00029 #include <kfileitem.h> 00030 #include <kapplication.h> 00031 #include <kdirlister.h> 00032 #include <kstandarddirs.h> 00033 #include <kiconloader.h> 00034 #include <konq_settings.h> 00035 #include <klocale.h> 00036 #include <kdebug.h> 00037 00038 #include "kivdirectoryoverlay.h" 00039 00040 KIVDirectoryOverlay::KIVDirectoryOverlay(KFileIVI* directory) 00041 : m_lister(0), m_foundItems(false), 00042 m_containsFolder(false), m_popularIcons(0) 00043 { 00044 if (!m_lister) 00045 { 00046 m_lister = new KDirLister; 00047 connect(m_lister, SIGNAL(completed()), SLOT(slotCompleted())); 00048 connect(m_lister, SIGNAL(newItems( const KFileItemList& )), SLOT(slotNewItems( const KFileItemList& ))); 00049 m_lister->setShowingDotFiles(false); 00050 } 00051 m_directory = directory; 00052 } 00053 00054 KIVDirectoryOverlay::~KIVDirectoryOverlay() 00055 { 00056 if (m_lister) m_lister->stop(); 00057 delete m_lister; 00058 delete m_popularIcons; 00059 } 00060 00061 void KIVDirectoryOverlay::start() 00062 { 00063 if ( m_directory->item()->isReadable() ) { 00064 m_popularIcons = new QDict<int>; 00065 m_popularIcons->setAutoDelete(true); 00066 m_lister->openURL(m_directory->item()->url()); 00067 } else { 00068 emit finished(); 00069 } 00070 } 00071 00072 void KIVDirectoryOverlay::timerEvent(QTimerEvent *) 00073 { 00074 m_lister->stop(); 00075 } 00076 00077 void KIVDirectoryOverlay::slotCompleted() 00078 { 00079 if (!m_popularIcons) return; 00080 00081 // Look through the histogram for the most popular mimetype 00082 QDictIterator<int> currentIcon( (*m_popularIcons) ); 00083 unsigned int best = 0; 00084 unsigned int total = 0; 00085 for ( ; currentIcon.current(); ++currentIcon ) { 00086 unsigned int currentCount = (*currentIcon.current()); 00087 total += currentCount; 00088 if ( best < currentCount ) { 00089 best = currentCount; 00090 m_bestIcon = currentIcon.currentKey(); 00091 } 00092 } 00093 00094 // Only show folder if there's no other candidate. Most folders contain 00095 // folders. We know this. 00096 if ( m_bestIcon.isNull() && m_containsFolder ) { 00097 m_bestIcon = "folder"; 00098 } 00099 00100 if ( best * 2 < total ) { 00101 m_bestIcon = "kmultiple"; 00102 } 00103 00104 if (!m_bestIcon.isNull()) { 00105 m_directory->setOverlay(m_bestIcon); 00106 } 00107 00108 delete m_popularIcons; 00109 m_popularIcons = 0; 00110 00111 emit finished(); 00112 } 00113 00114 void KIVDirectoryOverlay::slotNewItems( const KFileItemList& items ) 00115 { 00116 if ( !m_popularIcons) return; 00117 00118 KFileItemListIterator files( items ); 00119 00120 KFileItem* file; 00121 for ( ; (file = files.current()) != 0; ++files ) { 00122 if ( file -> isFile() ) { 00123 00124 QString iconName = file -> iconName(); 00125 if (!iconName) continue; 00126 00127 int* iconCount = m_popularIcons -> find( file -> iconName() ); 00128 if (!iconCount) { 00129 iconCount = new int(0); 00130 Q_ASSERT(file); 00131 m_popularIcons -> insert(file -> iconName(), iconCount); 00132 } 00133 (*iconCount)++; 00134 } else if ( file -> isDir() ) { 00135 m_containsFolder = true; 00136 } 00137 } 00138 00139 m_foundItems = true; 00140 } 00141 00142 #include "kivdirectoryoverlay.moc"
KDE Logo
This file is part of the documentation for libkonq Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Aug 31 00:02:07 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003