kfilespeedbar.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
00003 
00004     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, version 2.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "kfilespeedbar.h"
00020 #include "config-kfile.h"
00021 
00022 #include <qdir.h>
00023 #include <qfile.h>
00024 #include <qtextcodec.h>
00025 #include <qtextstream.h>
00026 
00027 #include <kconfig.h>
00028 #include <kglobal.h>
00029 #include <kglobalsettings.h>
00030 #include <klocale.h>
00031 #include <kprotocolinfo.h>
00032 #include <kstandarddirs.h>
00033 #include <kurl.h>
00034 
00035 KFileSpeedBar::KFileSpeedBar( QWidget *parent, const char *name )
00036     : KURLBar( true, parent, name )
00037 {
00038     KConfig *config = KGlobal::config();
00039     KConfigGroupSaver cs( config, ConfigGroup );
00040     m_initializeSpeedbar = config->readBoolEntry( "Set speedbar defaults",
00041                                                    true );
00042     setIconSize(KIcon::SizeSmallMedium);
00043     readConfig( KGlobal::config(), "KFileDialog Speedbar" );
00044 
00045     if ( m_initializeSpeedbar )
00046     {
00047         KURL u;
00048         u.setPath( KGlobalSettings::desktopPath() );
00049         insertItem( u, i18n("Desktop"), false );
00050 
00051 //TODO: win32
00052         if ((KGlobalSettings::documentPath() != (QDir::homeDirPath()+"/")) &&
00053             QDir(KGlobalSettings::documentPath()).exists())
00054         {
00055             u.setPath( KGlobalSettings::documentPath() );
00056             insertItem( u, i18n("Documents"), false, "folder_txt" );
00057         }
00058 
00059         u.setPath( QDir::homeDirPath() );
00060         insertItem( u, i18n("Home Folder"), false,
00061                                "folder_home" );
00062 
00063         u = "media:/";
00064         if ( KProtocolInfo::isKnownProtocol( u ) )
00065             insertItem( u, i18n("Storage Media"), false,
00066                                    KProtocolInfo::icon( "media" ) );
00067 
00068         if (  QFile::exists(  QDir::homeDirPath()+"/.config/user-dirs.dirs" ) )
00069         {
00070             QString download, music, pictures, videos, templates, publicShares;
00071 
00072             QFile f(  QDir::homeDirPath()+"/.config/user-dirs.dirs" );
00073             if (!f.open(IO_ReadOnly))
00074                 return;
00075 
00076             QTextStream s( &f );
00077             s.setCodec( QTextCodec::codecForLocale() );
00078 
00079             // read the xdg user dirs
00080             QString line = s.readLine();
00081             while (!line.isNull())
00082             {
00083                 if (line.startsWith("XDG_DOWNLOAD_DIR="))
00084                     download = line.remove("XDG_DOWNLOAD_DIR=").remove("\"").replace("$HOME", QDir::homeDirPath());
00085                 else if (line.startsWith("XDG_MUSIC_DIR="))
00086                     music = line.remove("XDG_MUSIC_DIR=").remove("\"").replace("$HOME", QDir::homeDirPath());
00087                 else if (line.startsWith("XDG_PICTURES_DIR="))
00088                     pictures = line.remove("XDG_PICTURES_DIR=").remove("\"").replace("$HOME", QDir::homeDirPath());
00089                 else if (line.startsWith("XDG_VIDEOS_DIR="))
00090                     videos = line.remove("XDG_VIDEOS_DIR=").remove("\"").replace("$HOME", QDir::homeDirPath());
00091                 else if (line.startsWith("XDG_TEMPLATES_DIR="))
00092                     templates = line.remove("XDG_TEMPLATES_DIR=").remove("\"").replace("$HOME", QDir::homeDirPath());
00093                 else if (line.startsWith("XDG_PUBLICSHARES_DIR="))
00094                     publicShares = line.remove("XDG_PUBLICSHARES_DIR=").remove("\"").replace("$HOME", QDir::homeDirPath());
00095 
00096                 line = s.readLine();
00097             }
00098             // now add in the speedbar
00099             if (!download.isEmpty())
00100                 insertItem( download, i18n( "Download" ), false, "folder_html" );
00101             if (!music.isEmpty())
00102                 insertItem( music, i18n( "Music" ), false, "folder_sound" );
00103             if (!pictures.isEmpty())
00104                 insertItem( pictures, i18n( "Pictures" ), false, "folder_image" );
00105             if (!videos.isEmpty())
00106                 insertItem( videos, i18n( "Videos" ), false, "folder_video" );
00107             if (!templates.isEmpty())
00108                 insertItem( templates, i18n( "Templates" ), false, "folder_video" );
00109             if (!publicShares.isEmpty())
00110                 insertItem( publicShares, i18n( "Public" ), false, "folder_video" );
00111         }
00112         
00113         u = "remote:/";
00114         if ( KProtocolInfo::isKnownProtocol( u ) )
00115             insertItem( u, i18n("Network Folders"), false,
00116                                    KProtocolInfo::icon( "remote" ) );
00117     }
00118 }
00119 
00120 KFileSpeedBar::~KFileSpeedBar()
00121 {
00122 }
00123 
00124 void KFileSpeedBar::save( KConfig *config )
00125 {
00126     if ( m_initializeSpeedbar && isModified() )
00127     {
00128         KConfigGroup conf( config, ConfigGroup );
00129         // write to kdeglobals
00130         conf.writeEntry( "Set speedbar defaults", false, true, true );
00131     }
00132 
00133     writeConfig( config, "KFileDialog Speedbar" );
00134 }
00135 
00136 QSize KFileSpeedBar::sizeHint() const
00137 {
00138     QSize sizeHint = KURLBar::sizeHint();
00139     int ems = fontMetrics().width("mmmmmmmmmmmm");
00140     if (sizeHint.width() < ems)
00141     {
00142         sizeHint.setWidth(ems);
00143     }
00144     return sizeHint;
00145 }
00146 
00147 #include "kfilespeedbar.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys