libkonq Library API Documentation

konq_propsview.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Faure David <faure@kde.org>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU 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 program 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     General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "konq_propsview.h"
00021 
00022 #include <kdebug.h>
00023 #include <kstandarddirs.h>
00024 #include <kpixmap.h>
00025 #include <qpixmapcache.h>
00026 #include <qiconview.h>
00027 #include <unistd.h>
00028 #include <qfile.h>
00029 #include <iostream>
00030 #include <ktrader.h>
00031 #include <kinstance.h>
00032 #include <assert.h>
00033 
00034 #include <ksimpleconfig.h>
00035 
00036 static QPixmap wallpaperPixmap( const QString & _wallpaper )
00037 {
00038     QString key = "wallpapers/";
00039     key += _wallpaper;
00040     KPixmap pix;
00041 
00042     if ( QPixmapCache::find( key, pix ) )
00043       return pix;
00044 
00045     QString path = locate("tiles", _wallpaper);
00046     if (path.isEmpty())
00047         path = locate("wallpaper", _wallpaper);
00048     if (!path.isEmpty())
00049     {
00050       // This looks really ugly, especially on an 8bit display.
00051       // I'm not sure what it's good for.
00052       // Anyway, if you change it here, keep konq_bgnddlg in sync (David)
00053       // pix.load( path, 0, KPixmap::LowColor );
00054       pix.load( path );
00055       if ( pix.isNull() )
00056         kdWarning(1203) << "Could not load wallpaper " << path << endl;
00057       else
00058         QPixmapCache::insert( key, pix );
00059       return pix;
00060     } else kdWarning(1203) << "Couldn't locate wallpaper " << _wallpaper << endl;
00061     return QPixmap();
00062 }
00063 
00064 struct KonqPropsView::Private
00065 {
00066    QStringList* previewsToShow;
00067    bool previewsEnabled:1;
00068    bool caseInsensitiveSort:1;
00069 };
00070 
00071 KonqPropsView::KonqPropsView( KInstance * instance, KonqPropsView * defaultProps )
00072     : m_bSaveViewPropertiesLocally( false ), // will be overriden by setSave... anyway
00073     // if this is the default properties instance, then keep config object for saving
00074     m_dotDirExists( true ), // HACK so that enterDir returns true initially
00075     m_currentConfig( defaultProps ? 0L : instance->config() ),
00076     m_defaultProps( defaultProps )
00077 {
00078 
00079   KConfig *config = instance->config();
00080   KConfigGroupSaver cgs(config, "Settings");
00081 
00082   d = new Private;
00083   d->previewsToShow = 0;
00084   d->caseInsensitiveSort=config->readBoolEntry( "CaseInsensitiveSort", false );
00085 
00086   m_iIconSize = config->readNumEntry( "IconSize", 0 );
00087   m_iItemTextPos = config->readNumEntry( "ItemTextPos", QIconView::Bottom );
00088   m_bShowDot = config->readBoolEntry( "ShowDotFiles", false );
00089   m_bShowDirectoryOverlays = config->readBoolEntry( "ShowDirectoryOverlays", false );
00090 
00091   m_dontPreview = config->readListEntry( "DontPreview" );
00092   m_dontPreview.remove("audio/"); //Use the separate setting.
00093   //We default to this off anyway, so it's no harm to remove this
00094 
00095   //The setting for sound previews is stored separately, so we can force
00096   //the default-to-off bias to propagate up.
00097   if (!config->readBoolEntry("EnableSoundPreviews", false))
00098   {
00099     if (!m_dontPreview.contains("audio/"))
00100       m_dontPreview.append("audio/");
00101   }
00102 
00103   d->previewsEnabled = config->readBoolEntry( "PreviewsEnabled", true );
00104 
00105   m_textColor = config->readColorEntry( "TextColor" ); // will be set to QColor() if not found
00106   m_bgColor = config->readColorEntry( "BgColor" ); // will be set to QColor() if not found
00107   m_bgPixmapFile = config->readPathEntry( "BgImage" );
00108   //kdDebug(1203) << "KonqPropsView::KonqPropsView from \"config\" : BgImage=" << m_bgPixmapFile << endl;
00109 
00110   // colorsConfig is either the local file (.directory) or the application global file
00111   // (we want the same colors for all types of view)
00112   // The code above reads from the view's config file, for compatibility only.
00113   // So now we read the settings from the app global file, if this is the default props
00114   if (!defaultProps)
00115   {
00116       KConfigGroupSaver cgs2(KGlobal::config(), "Settings");
00117       m_textColor = KGlobal::config()->readColorEntry( "TextColor", &m_textColor );
00118       m_bgColor = KGlobal::config()->readColorEntry( "BgColor", &m_bgColor );
00119       m_bgPixmapFile = KGlobal::config()->readPathEntry( "BgImage", m_bgPixmapFile );
00120       //kdDebug(1203) << "KonqPropsView::KonqPropsView from KGlobal : BgImage=" << m_bgPixmapFile << endl;
00121   }
00122 
00123   KGlobal::dirs()->addResourceType("tiles",
00124                                    KGlobal::dirs()->kde_default("data") + "konqueror/tiles/");
00125 }
00126 
00127 bool KonqPropsView::isCaseInsensitiveSort() const
00128 {
00129    return d->caseInsensitiveSort;
00130 }
00131 
00132 
00133 KConfigBase * KonqPropsView::currentConfig()
00134 {
00135     if ( !m_currentConfig )
00136     {
00137         // 0L ? This has to be a non-default save-locally instance...
00138         assert ( m_bSaveViewPropertiesLocally );
00139         assert ( !isDefaultProperties() );
00140 
00141         if (!dotDirectory.isEmpty())
00142             m_currentConfig = new KSimpleConfig( dotDirectory );
00143         // the "else" is when we want to save locally but this is a remote URL -> no save
00144     }
00145     return m_currentConfig;
00146 }
00147 
00148 KConfigBase * KonqPropsView::currentColorConfig()
00149 {
00150     // Saving locally ?
00151     if ( m_bSaveViewPropertiesLocally && !isDefaultProperties() )
00152         return currentConfig(); // Will create it if necessary
00153     else
00154         // Save color settings in app's file, not in view's file
00155         return KGlobal::config();
00156 }
00157 
00158 KonqPropsView::~KonqPropsView()
00159 {
00160    delete d->previewsToShow;
00161    delete d;
00162    d=0;
00163 }
00164 
00165 bool KonqPropsView::enterDir( const KURL & dir )
00166 {
00167   //kdDebug(1203) << "enterDir " << dir.prettyURL() << endl;
00168   // Can't do that with default properties
00169   assert( !isDefaultProperties() );
00170 
00171   // Check for .directory
00172   KURL u ( dir );
00173   u.addPath(".directory");
00174   bool dotDirExists = u.isLocalFile() && QFile::exists( u.path() );
00175   dotDirectory = u.isLocalFile() ? u.path() : QString::null;
00176 
00177   // Revert to default setting first - unless there is no .directory
00178   // in the previous dir nor in this one (then we can keep the current settings)
00179   if (dotDirExists || m_dotDirExists)
00180   {
00181     m_iIconSize = m_defaultProps->iconSize();
00182     m_iItemTextPos = m_defaultProps->itemTextPos();
00183     m_bShowDot = m_defaultProps->isShowingDotFiles();
00184     d->caseInsensitiveSort=m_defaultProps->isCaseInsensitiveSort();
00185     m_dontPreview = m_defaultProps->m_dontPreview;
00186     m_textColor = m_defaultProps->m_textColor;
00187     m_bgColor = m_defaultProps->m_bgColor;
00188     m_bgPixmapFile = m_defaultProps->bgPixmapFile();
00189   }
00190 
00191   if (dotDirExists)
00192   {
00193     //kdDebug(1203) << "Found .directory file" << endl;
00194     KSimpleConfig * config = new KSimpleConfig( dotDirectory, true );
00195     config->setGroup("URL properties");
00196 
00197     m_iIconSize = config->readNumEntry( "IconSize", m_iIconSize );
00198     m_iItemTextPos = config->readNumEntry( "ItemTextPos", m_iItemTextPos );
00199     m_bShowDot = config->readBoolEntry( "ShowDotFiles", m_bShowDot );
00200     d->caseInsensitiveSort=config->readBoolEntry("CaseInsensitiveSort",d->caseInsensitiveSort);
00201     m_bShowDirectoryOverlays = config->readBoolEntry( "ShowDirectoryOverlays", m_bShowDirectoryOverlays );
00202     if (config->hasKey( "DontPreview" ))
00203     {
00204         m_dontPreview = config->readListEntry( "DontPreview" );
00205 
00206         //If the .directory file says something about sound previews,
00207         //obey it, otherwise propagate the setting up from the defaults
00208         //All this really should be split into a per-thumbnail setting,
00209         //but that's too invasive at this point
00210         if (config->hasKey("EnableSoundPreviews"))
00211         {
00212 
00213             if (!config->readBoolEntry("EnableSoundPreviews", false))
00214                 if (!m_dontPreview.contains("audio/"))
00215                     m_dontPreview.append("audio/");
00216         }
00217         else
00218         {
00219             if (m_defaultProps->m_dontPreview.contains("audio/"))
00220                 if (!m_dontPreview.contains("audio/"))
00221                     m_dontPreview.append("audio/");
00222         }
00223     }
00224 
00225 
00226 
00227     m_textColor = config->readColorEntry( "TextColor", &m_textColor );
00228     m_bgColor = config->readColorEntry( "BgColor", &m_bgColor );
00229     m_bgPixmapFile = config->readPathEntry( "BgImage", m_bgPixmapFile );
00230     //kdDebug(1203) << "KonqPropsView::enterDir m_bgPixmapFile=" << m_bgPixmapFile << endl;
00231     d->previewsEnabled = config->readBoolEntry( "PreviewsEnabled", d->previewsEnabled );
00232     delete config;
00233   }
00234   //if there is or was a .directory then the settings probably have changed
00235   bool configChanged=(m_dotDirExists|| dotDirExists);
00236   m_dotDirExists = dotDirExists;
00237   m_currentConfig = 0L; // new dir, not current config for saving yet
00238   //kdDebug(1203) << "KonqPropsView::enterDir returning " << configChanged << endl;
00239   return configChanged;
00240 }
00241 
00242 void KonqPropsView::setSaveViewPropertiesLocally( bool value )
00243 {
00244     assert( !isDefaultProperties() );
00245     //kdDebug(1203) << "KonqPropsView::setSaveViewPropertiesLocally " << value << endl;
00246 
00247     if ( m_bSaveViewPropertiesLocally )
00248         delete m_currentConfig; // points to a KSimpleConfig
00249 
00250     m_bSaveViewPropertiesLocally = value;
00251     m_currentConfig = 0L; // mark as dirty
00252 }
00253 
00254 void KonqPropsView::setIconSize( int size )
00255 {
00256     m_iIconSize = size;
00257     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00258         m_defaultProps->setIconSize( size );
00259     else if (currentConfig())
00260     {
00261         KConfigGroupSaver cgs(currentConfig(), currentGroup());
00262         currentConfig()->writeEntry( "IconSize", m_iIconSize );
00263         currentConfig()->sync();
00264     }
00265 }
00266 
00267 void KonqPropsView::setItemTextPos( int pos )
00268 {
00269     m_iItemTextPos = pos;
00270     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00271         m_defaultProps->setItemTextPos( pos );
00272     else if (currentConfig())
00273     {
00274         KConfigGroupSaver cgs(currentConfig(), currentGroup());
00275         currentConfig()->writeEntry( "ItemTextPos", m_iItemTextPos );
00276         currentConfig()->sync();
00277     }
00278 }
00279 
00280 void KonqPropsView::setShowingDotFiles( bool show )
00281 {
00282     kdDebug(1203) << "KonqPropsView::setShowingDotFiles " << show << endl;
00283     m_bShowDot = show;
00284     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00285     {
00286         kdDebug(1203) << "Saving in default properties" << endl;
00287         m_defaultProps->setShowingDotFiles( show );
00288     }
00289     else if (currentConfig())
00290     {
00291         kdDebug(1203) << "Saving in current config" << endl;
00292         KConfigGroupSaver cgs(currentConfig(), currentGroup());
00293         currentConfig()->writeEntry( "ShowDotFiles", m_bShowDot );
00294         currentConfig()->sync();
00295     }
00296 }
00297 
00298 void KonqPropsView::setCaseInsensitiveSort( bool on )
00299 {
00300     kdDebug(1203) << "KonqPropsView::setCaseInsensitiveSort " << on << endl;
00301     d->caseInsensitiveSort = on;
00302     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00303     {
00304         kdDebug(1203) << "Saving in default properties" << endl;
00305         m_defaultProps->setCaseInsensitiveSort( on );
00306     }
00307     else if (currentConfig())
00308     {
00309         kdDebug(1203) << "Saving in current config" << endl;
00310         KConfigGroupSaver cgs(currentConfig(), currentGroup());
00311         currentConfig()->writeEntry( "CaseInsensitiveSort", d->caseInsensitiveSort );
00312         currentConfig()->sync();
00313     }
00314 }
00315 
00316 void KonqPropsView::setShowingDirectoryOverlays( bool show )
00317 {
00318     kdDebug(1203) << "KonqPropsView::setShowingDirectoryOverlays " << show << endl;
00319     m_bShowDirectoryOverlays = show;
00320     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00321     {
00322         kdDebug(1203) << "Saving in default properties" << endl;
00323         m_defaultProps->setShowingDirectoryOverlays( show );
00324     }
00325     else if (currentConfig())
00326     {
00327         kdDebug(1203) << "Saving in current config" << endl;
00328         KConfigGroupSaver cgs(currentConfig(), currentGroup());
00329         currentConfig()->writeEntry( "ShowDirectoryOverlays", m_bShowDirectoryOverlays );
00330         currentConfig()->sync();
00331     }
00332 }
00333 
00334 void KonqPropsView::setShowingPreview( const QString &preview, bool show )
00335 {
00336     if ( m_dontPreview.contains( preview ) != show )
00337         return;
00338     else if ( show )
00339         m_dontPreview.remove( preview );
00340     else
00341         m_dontPreview.append( preview );
00342     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00343         m_defaultProps->setShowingPreview( preview, show );
00344     else if (currentConfig())
00345     {
00346         KConfigGroupSaver cgs(currentConfig(), currentGroup());
00347 
00348         //Audio is special-cased, as we use a binary setting
00349         //for it to get it to follow the defaults right.
00350         bool audioEnabled = !m_dontPreview.contains("audio/");
00351 
00352         //Don't write it out into the DontPreview line
00353         if (!audioEnabled)
00354             m_dontPreview.remove("audio/");
00355         currentConfig()->writeEntry( "DontPreview", m_dontPreview );
00356         currentConfig()->writeEntry( "EnableSoundPreviews", audioEnabled );
00357         currentConfig()->sync();
00358         if (!audioEnabled)
00359             m_dontPreview.append("audio/");
00360 
00361     }
00362 
00363     delete d->previewsToShow;
00364     d->previewsToShow = 0;
00365 }
00366 
00367 void KonqPropsView::setShowingPreview( bool show )
00368 {
00369     d->previewsEnabled = show;
00370 
00371     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00372     {
00373         kdDebug(1203) << "Saving in default properties" << endl;
00374         m_defaultProps-> setShowingPreview( show );
00375     }
00376     else if (currentConfig())
00377     {
00378         kdDebug(1203) << "Saving in current config" << endl;
00379         KConfigGroupSaver cgs(currentConfig(), currentGroup());
00380         currentConfig()->writeEntry( "PreviewsEnabled", d->previewsEnabled );
00381         currentConfig()->sync();
00382     }
00383 
00384     delete d->previewsToShow;
00385     d->previewsToShow = 0;
00386 }
00387 
00388 bool KonqPropsView::isShowingPreview()
00389 {
00390     return d->previewsEnabled;
00391 }
00392 
00393 void KonqPropsView::setBgColor( const QColor & color )
00394 {
00395     m_bgColor = color;
00396     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00397     {
00398         m_defaultProps->setBgColor( color );
00399     }
00400     else
00401     {
00402         KConfigBase * colorConfig = currentColorConfig();
00403         if (colorConfig) // 0L when saving locally but remote URL
00404         {
00405             KConfigGroupSaver cgs(colorConfig, currentGroup());
00406             colorConfig->writeEntry( "BgColor", m_bgColor );
00407             colorConfig->sync();
00408         }
00409     }
00410 }
00411 
00412 const QColor & KonqPropsView::bgColor( QWidget * widget ) const
00413 {
00414     if ( !m_bgColor.isValid() )
00415         return widget->colorGroup().base();
00416     else
00417         return m_bgColor;
00418 }
00419 
00420 void KonqPropsView::setTextColor( const QColor & color )
00421 {
00422     m_textColor = color;
00423     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00424     {
00425         m_defaultProps->setTextColor( color );
00426     }
00427     else
00428     {
00429         KConfigBase * colorConfig = currentColorConfig();
00430         if (colorConfig) // 0L when saving locally but remote URL
00431         {
00432             KConfigGroupSaver cgs(colorConfig, currentGroup());
00433             colorConfig->writeEntry( "TextColor", m_textColor );
00434             colorConfig->sync();
00435         }
00436     }
00437 }
00438 
00439 const QColor & KonqPropsView::textColor( QWidget * widget ) const
00440 {
00441     if ( !m_textColor.isValid() )
00442         return widget->colorGroup().text();
00443     else
00444         return m_textColor;
00445 }
00446 
00447 void KonqPropsView::setBgPixmapFile( const QString & file )
00448 {
00449     m_bgPixmapFile = file;
00450 
00451     if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
00452     {
00453         m_defaultProps->setBgPixmapFile( file );
00454     }
00455     else
00456     {
00457         KConfigBase * colorConfig = currentColorConfig();
00458         if (colorConfig) // 0L when saving locally but remote URL
00459         {
00460             KConfigGroupSaver cgs(colorConfig, currentGroup());
00461             colorConfig->writeEntry( "BgImage", file );
00462             colorConfig->sync();
00463         }
00464     }
00465 }
00466 
00467 QPixmap KonqPropsView::loadPixmap() const
00468 {
00469     //kdDebug(1203) << "KonqPropsView::loadPixmap " << m_bgPixmapFile << endl;
00470     QPixmap bgPixmap;
00471     if ( !m_bgPixmapFile.isEmpty() )
00472         bgPixmap = wallpaperPixmap( m_bgPixmapFile );
00473     return bgPixmap;
00474 }
00475 
00476 void KonqPropsView::applyColors(QWidget * widget) const
00477 {
00478     //kdDebug(1203) << "KonqPropsView::applyColors " << (void*)this << endl;
00479     QColorGroup a = widget->palette().active();
00480     QColorGroup d = widget->palette().disabled(); // is this one ever used ?
00481     QColorGroup i = widget->palette().inactive(); // is this one ever used ?
00482     bool setPaletteNeeded = false;
00483 
00484     if ( m_bgPixmapFile.isEmpty() )
00485     {
00486         QColor col = bgColor(widget);
00487         a.setColor( QColorGroup::Base, col );
00488         d.setColor( QColorGroup::Base, col );
00489         i.setColor( QColorGroup::Base, col );
00490         widget->setBackgroundColor( col );
00491         setPaletteNeeded = true;
00492     }
00493     else
00494     {
00495         widget->setPaletteBackgroundPixmap( loadPixmap() );
00496     }
00497 
00498     if ( m_textColor.isValid() )
00499     {
00500         a.setColor( QColorGroup::Text, m_textColor );
00501         d.setColor( QColorGroup::Text, m_textColor );
00502         i.setColor( QColorGroup::Text, m_textColor );
00503         setPaletteNeeded = true;
00504     }
00505 
00506     // Avoid calling setPalette if we are fine with the default values.
00507     // This makes us react to the palette-change event accordingly.
00508     if ( setPaletteNeeded )
00509         widget->setPalette( QPalette( a, d, i ) );
00510 }
00511 
00512 const QStringList& KonqPropsView::previewSettings()
00513 {
00514     if ( ! d->previewsToShow )
00515     {
00516         d->previewsToShow = new QStringList;
00517 
00518         if (d->previewsEnabled) {
00519             KTrader::OfferList plugins = KTrader::self()->query( "ThumbCreator" );
00520             for ( KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it )
00521             {
00522             QString name = (*it)->desktopEntryName();
00523             if ( ! m_dontPreview.contains(name) )
00524                     d->previewsToShow->append( name );
00525             }
00526             if ( ! m_dontPreview.contains( "audio/" ) )
00527             d->previewsToShow->append( "audio/" );
00528         }
00529     }
00530 
00531     return *(d->previewsToShow);
00532 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Thu Jan 29 23:03:28 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001