summaryrefslogtreecommitdiffstats
path: root/libkonq/konq_propsview.cc
blob: 303fe044ff466ab62722afaa89d651f3e93be712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Faure David <faure@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "konq_propsview.h"
#include "konq_settings.h"

#include <kdebug.h>
#include <kstandarddirs.h>
#include <kpixmap.h>
#include <tqpixmapcache.h>
#include <tqiconview.h>
#include <unistd.h>
#include <tqfile.h>
#include <iostream>
#include <ktrader.h>
#include <kinstance.h>
#include <assert.h>

#include <ksimpleconfig.h>

static TQPixmap wallpaperPixmap( const TQString & _wallpaper )
{
    TQString key = "wallpapers/";
    key += _wallpaper;
    KPixmap pix;

    if ( TQPixmapCache::find( key, pix ) )
      return pix;

    TQString path = locate("tiles", _wallpaper);
    if (path.isEmpty())
        path = locate("wallpaper", _wallpaper);
    if (!path.isEmpty())
    {
      // This looks really ugly, especially on an 8bit display.
      // I'm not sure what it's good for.
      // Anyway, if you change it here, keep konq_bgnddlg in sync (David)
      // pix.load( path, 0, KPixmap::LowColor );
      pix.load( path );
      if ( pix.isNull() )
        kdWarning(1203) << "Could not load wallpaper " << path << endl;
      else
        TQPixmapCache::insert( key, pix );
      return pix;
    } else kdWarning(1203) << "Couldn't locate wallpaper " << _wallpaper << endl;
    return TQPixmap();
}

struct KonqPropsView::Private
{
   TQStringList* previewsToShow;
   bool previewsEnabled:1;
   bool caseInsensitiveSort:1;
   bool dirsfirst:1;
   bool descending:1;
   TQString sortcriterion;
};

KonqPropsView::KonqPropsView( KInstance * instance, KonqPropsView * defaultProps )
    : m_bSaveViewPropertiesLocally( false ), // will be overridden by setSave... anyway
    // if this is the default properties instance, then keep config object for saving
    m_dotDirExists( true ), // HACK so that enterDir returns true initially
    m_currentConfig( defaultProps ? 0L : instance->config() ),
    m_defaultProps( defaultProps )
{

  KConfig *config = instance->config();
  KConfigGroupSaver cgs(config, "Settings");

  d = new Private;
  d->previewsToShow = 0;
  d->caseInsensitiveSort=config->readBoolEntry( "CaseInsensitiveSort", true );

  m_iIconSize = config->readNumEntry( "IconSize", 0 );
  m_iItemTextPos = config->readNumEntry( "ItemTextPos", TQIconView::Bottom );
  d->sortcriterion = config->readEntry( "SortingCriterion", "sort_nci" );
  d->dirsfirst = config->readBoolEntry( "SortDirsFirst", true );
  d->descending = config->readBoolEntry( "SortDescending", false );
  m_bShowDot = config->readBoolEntry( "ShowDotFiles", false );
  m_bShowDirectoryOverlays = config->readBoolEntry( "ShowDirectoryOverlays", false );

  m_dontPreview = config->readListEntry( "DontPreview" );
  m_dontPreview.remove("audio/"); //Use the separate setting.
  //We default to this off anyway, so it's no harm to remove this

  //The setting for sound previews is stored separately, so we can force
  //the default-to-off bias to propagate up.
  if (!config->readBoolEntry("EnableSoundPreviews", false))
  {
    if (!m_dontPreview.contains("audio/"))
      m_dontPreview.append("audio/");
  }

  d->previewsEnabled = config->readBoolEntry( "PreviewsEnabled", true );

  TQColor tc = KonqFMSettings::settings()->normalTextColor();
  m_textColor = config->readColorEntry( "TextColor", &tc );
  m_bgColor = config->readColorEntry( "BgColor" ); // will be set to TQColor() if not found
  m_bgPixmapFile = config->readPathEntry( "BgImage" );
  //kdDebug(1203) << "KonqPropsView::KonqPropsView from \"config\" : BgImage=" << m_bgPixmapFile << endl;

  // colorsConfig is either the local file (.directory) or the application global file
  // (we want the same colors for all types of view)
  // The code above reads from the view's config file, for compatibility only.
  // So now we read the settings from the app global file, if this is the default props
  if (!defaultProps)
  {
      KConfigGroupSaver cgs2(KGlobal::config(), "Settings");
      m_textColor = KGlobal::config()->readColorEntry( "TextColor", &m_textColor );
      m_bgColor = KGlobal::config()->readColorEntry( "BgColor", &m_bgColor );
      m_bgPixmapFile = KGlobal::config()->readPathEntry( "BgImage", m_bgPixmapFile );
      //kdDebug(1203) << "KonqPropsView::KonqPropsView from KGlobal : BgImage=" << m_bgPixmapFile << endl;
  }

  KGlobal::dirs()->addResourceType("tiles",
                                   KGlobal::dirs()->kde_default("data") + "konqueror/tiles/");
}

bool KonqPropsView::isCaseInsensitiveSort() const
{
   return d->caseInsensitiveSort;
}

bool KonqPropsView::isDirsFirst() const
{
   return d->dirsfirst;
}

bool KonqPropsView::isDescending() const
{
   return d->descending;
}

KConfigBase * KonqPropsView::currentConfig()
{
    if ( !m_currentConfig )
    {
        // 0L ? This has to be a non-default save-locally instance...
        assert ( m_bSaveViewPropertiesLocally );
        assert ( !isDefaultProperties() );

        if (!dotDirectory.isEmpty())
            m_currentConfig = new KSimpleConfig( dotDirectory );
        // the "else" is when we want to save locally but this is a remote URL -> no save
    }
    return m_currentConfig;
}

KConfigBase * KonqPropsView::currentColorConfig()
{
    // Saving locally ?
    if ( m_bSaveViewPropertiesLocally && !isDefaultProperties() )
        return currentConfig(); // Will create it if necessary
    else
        // Save color settings in app's file, not in view's file
        return KGlobal::config();
}

KonqPropsView::~KonqPropsView()
{
   delete d->previewsToShow;
   delete d;
   d=0;
}

bool KonqPropsView::enterDir( const KURL & dir )
{
  //kdDebug(1203) << "enterDir " << dir.prettyURL() << endl;
  // Can't do that with default properties
  assert( !isDefaultProperties() );

  // Check for .directory
  KURL u ( dir );
  u.addPath(".directory");
  bool dotDirExists = u.isLocalFile() && TQFile::exists( u.path() );
  dotDirectory = u.isLocalFile() ? u.path() : TQString::null;

  // Revert to default setting first - unless there is no .directory
  // in the previous dir nor in this one (then we can keep the current settings)
  if (dotDirExists || m_dotDirExists)
  {
    m_iIconSize = m_defaultProps->iconSize();
    m_iItemTextPos = m_defaultProps->itemTextPos();
    d->sortcriterion = m_defaultProps->sortCriterion();
    d->dirsfirst = m_defaultProps->isDirsFirst();
    d->descending = m_defaultProps->isDescending();
    m_bShowDot = m_defaultProps->isShowingDotFiles();
    d->caseInsensitiveSort=m_defaultProps->isCaseInsensitiveSort();
    m_dontPreview = m_defaultProps->m_dontPreview;
    m_textColor = m_defaultProps->m_textColor;
    m_bgColor = m_defaultProps->m_bgColor;
    m_bgPixmapFile = m_defaultProps->bgPixmapFile();
  }

  if (dotDirExists)
  {
    //kdDebug(1203) << "Found .directory file" << endl;
    KSimpleConfig * config = new KSimpleConfig( dotDirectory, true );
    config->setGroup("URL properties");

    m_iIconSize = config->readNumEntry( "IconSize", m_iIconSize );
    m_iItemTextPos = config->readNumEntry( "ItemTextPos", m_iItemTextPos );
    d->sortcriterion = config->readEntry( "SortingCriterion" , d->sortcriterion );
    d->dirsfirst = config->readBoolEntry( "SortDirsFirst", d->dirsfirst );
    d->descending = config->readBoolEntry( "SortDescending", d->descending );
    m_bShowDot = config->readBoolEntry( "ShowDotFiles", m_bShowDot );
    d->caseInsensitiveSort=config->readBoolEntry("CaseInsensitiveSort",d->caseInsensitiveSort);
    m_bShowDirectoryOverlays = config->readBoolEntry( "ShowDirectoryOverlays", m_bShowDirectoryOverlays );
    if (config->hasKey( "DontPreview" ))
    {
        m_dontPreview = config->readListEntry( "DontPreview" );

        //If the .directory file says something about sound previews,
        //obey it, otherwise propagate the setting up from the defaults
        //All this really should be split into a per-thumbnail setting,
        //but that's too invasive at this point
        if (config->hasKey("EnableSoundPreviews"))
        {

            if (!config->readBoolEntry("EnableSoundPreviews", false))
                if (!m_dontPreview.contains("audio/"))
                    m_dontPreview.append("audio/");
        }
        else
        {
            if (m_defaultProps->m_dontPreview.contains("audio/"))
                if (!m_dontPreview.contains("audio/"))
                    m_dontPreview.append("audio/");
        }
    }



    m_textColor = config->readColorEntry( "TextColor", &m_textColor );
    m_bgColor = config->readColorEntry( "BgColor", &m_bgColor );
    m_bgPixmapFile = config->readPathEntry( "BgImage", m_bgPixmapFile );
    //kdDebug(1203) << "KonqPropsView::enterDir m_bgPixmapFile=" << m_bgPixmapFile << endl;
    d->previewsEnabled = config->readBoolEntry( "PreviewsEnabled", d->previewsEnabled );
    delete config;
  }
  //if there is or was a .directory then the settings probably have changed
  bool configChanged=(m_dotDirExists|| dotDirExists);
  m_dotDirExists = dotDirExists;
  m_currentConfig = 0L; // new dir, not current config for saving yet
  //kdDebug(1203) << "KonqPropsView::enterDir returning " << configChanged << endl;
  return configChanged;
}

void KonqPropsView::setSaveViewPropertiesLocally( bool value )
{
    assert( !isDefaultProperties() );
    //kdDebug(1203) << "KonqPropsView::setSaveViewPropertiesLocally " << value << endl;

    if ( m_bSaveViewPropertiesLocally )
        delete m_currentConfig; // points to a KSimpleConfig

    m_bSaveViewPropertiesLocally = value;
    m_currentConfig = 0L; // mark as dirty
}

void KonqPropsView::setIconSize( int size )
{
    m_iIconSize = size;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
        m_defaultProps->setIconSize( size );
    else if (currentConfig())
    {
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "IconSize", m_iIconSize );
        currentConfig()->sync();
    }
}

void KonqPropsView::setItemTextPos( int pos )
{
    m_iItemTextPos = pos;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
        m_defaultProps->setItemTextPos( pos );
    else if (currentConfig())
    {
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "ItemTextPos", m_iItemTextPos );
        currentConfig()->sync();
    }
}

void KonqPropsView::setSortCriterion( const TQString &criterion )
{
    d->sortcriterion = criterion;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
        m_defaultProps->setSortCriterion( criterion );
    else if (currentConfig())
    {
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "SortingCriterion", d->sortcriterion );
        currentConfig()->sync();
    }
}

void KonqPropsView::setDirsFirst( bool first)
{
    d->dirsfirst = first;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
        m_defaultProps->setDirsFirst( first );
    else if (currentConfig())
    {
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "SortDirsFirst", d->dirsfirst );
        currentConfig()->sync();
    }
}

void KonqPropsView::setDescending( bool descend)
{
    d->descending = descend;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
        m_defaultProps->setDescending( descend );
    else if (currentConfig())
    {
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "SortDescending", d->descending );
        currentConfig()->sync();
    }
}

void KonqPropsView::setShowingDotFiles( bool show )
{
    kdDebug(1203) << "KonqPropsView::setShowingDotFiles " << show << endl;
    m_bShowDot = show;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
    {
        kdDebug(1203) << "Saving in default properties" << endl;
        m_defaultProps->setShowingDotFiles( show );
    }
    else if (currentConfig())
    {
        kdDebug(1203) << "Saving in current config" << endl;
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "ShowDotFiles", m_bShowDot );
        currentConfig()->sync();
    }
}

void KonqPropsView::setCaseInsensitiveSort( bool on )
{
    kdDebug(1203) << "KonqPropsView::setCaseInsensitiveSort " << on << endl;
    d->caseInsensitiveSort = on;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
    {
        kdDebug(1203) << "Saving in default properties" << endl;
        m_defaultProps->setCaseInsensitiveSort( on );
    }
    else if (currentConfig())
    {
        kdDebug(1203) << "Saving in current config" << endl;
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "CaseInsensitiveSort", d->caseInsensitiveSort );
        currentConfig()->sync();
    }
}

void KonqPropsView::setShowingDirectoryOverlays( bool show )
{
    kdDebug(1203) << "KonqPropsView::setShowingDirectoryOverlays " << show << endl;
    m_bShowDirectoryOverlays = show;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
    {
        kdDebug(1203) << "Saving in default properties" << endl;
        m_defaultProps->setShowingDirectoryOverlays( show );
    }
    else if (currentConfig())
    {
        kdDebug(1203) << "Saving in current config" << endl;
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "ShowDirectoryOverlays", m_bShowDirectoryOverlays );
        currentConfig()->sync();
    }
}

void KonqPropsView::setShowingPreview( const TQString &preview, bool show )
{
    if ( m_dontPreview.contains( preview ) != show )
        return;
    else if ( show )
        m_dontPreview.remove( preview );
    else
        m_dontPreview.append( preview );
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
        m_defaultProps->setShowingPreview( preview, show );
    else if (currentConfig())
    {
        KConfigGroupSaver cgs(currentConfig(), currentGroup());

        //Audio is special-cased, as we use a binary setting
        //for it to get it to follow the defaults right.
        bool audioEnabled = !m_dontPreview.contains("audio/");

        //Don't write it out into the DontPreview line
        if (!audioEnabled)
            m_dontPreview.remove("audio/");
        currentConfig()->writeEntry( "DontPreview", m_dontPreview );
        currentConfig()->writeEntry( "EnableSoundPreviews", audioEnabled );
        currentConfig()->sync();
        if (!audioEnabled)
            m_dontPreview.append("audio/");

    }

    delete d->previewsToShow;
    d->previewsToShow = 0;
}

void KonqPropsView::setShowingPreview( bool show )
{
    d->previewsEnabled = show;

    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
    {
        kdDebug(1203) << "Saving in default properties" << endl;
        m_defaultProps-> setShowingPreview( show );
    }
    else if (currentConfig())
    {
        kdDebug(1203) << "Saving in current config" << endl;
        KConfigGroupSaver cgs(currentConfig(), currentGroup());
        currentConfig()->writeEntry( "PreviewsEnabled", d->previewsEnabled );
        currentConfig()->sync();
    }

    delete d->previewsToShow;
    d->previewsToShow = 0;
}

bool KonqPropsView::isShowingPreview()
{
    return d->previewsEnabled;
}

void KonqPropsView::setBgColor( const TQColor & color )
{
    m_bgColor = color;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
    {
        m_defaultProps->setBgColor( color );
    }
    else
    {
        KConfigBase * colorConfig = currentColorConfig();
        if (colorConfig) // 0L when saving locally but remote URL
        {
            KConfigGroupSaver cgs(colorConfig, currentGroup());
            colorConfig->writeEntry( "BgColor", m_bgColor );
            colorConfig->sync();
        }
    }
}

const TQColor & KonqPropsView::bgColor( TQWidget * widget ) const
{
    if ( !m_bgColor.isValid() )
        return widget->colorGroup().base();
    else
        return m_bgColor;
}

void KonqPropsView::setTextColor( const TQColor & color )
{
    m_textColor = color;
    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
    {
        m_defaultProps->setTextColor( color );
    }
    else
    {
        KConfigBase * colorConfig = currentColorConfig();
        if (colorConfig) // 0L when saving locally but remote URL
        {
            KConfigGroupSaver cgs(colorConfig, currentGroup());
            colorConfig->writeEntry( "TextColor", m_textColor );
            colorConfig->sync();
        }
    }
}

const TQColor & KonqPropsView::textColor( TQWidget * widget ) const
{
    if ( !m_textColor.isValid() )
        return widget->colorGroup().text();
    else
        return m_textColor;
}

void KonqPropsView::setBgPixmapFile( const TQString & file )
{
    m_bgPixmapFile = file;

    if ( m_defaultProps && !m_bSaveViewPropertiesLocally )
    {
        m_defaultProps->setBgPixmapFile( file );
    }
    else
    {
        KConfigBase * colorConfig = currentColorConfig();
        if (colorConfig) // 0L when saving locally but remote URL
        {
            KConfigGroupSaver cgs(colorConfig, currentGroup());
            colorConfig->writePathEntry( "BgImage", file );
            colorConfig->sync();
        }
    }
}

TQPixmap KonqPropsView::loadPixmap() const
{
    //kdDebug(1203) << "KonqPropsView::loadPixmap " << m_bgPixmapFile << endl;
    TQPixmap bgPixmap;
    if ( !m_bgPixmapFile.isEmpty() )
        bgPixmap = wallpaperPixmap( m_bgPixmapFile );
    return bgPixmap;
}

void KonqPropsView::applyColors(TQWidget * widget) const
{
    if ( m_bgPixmapFile.isEmpty() )
        widget->setPaletteBackgroundColor( bgColor( widget ) );
    else
    {
        TQPixmap pix = loadPixmap();
        // don't set an null pixmap, as this leads to
        // undefined results with regards to the background of widgets
        // that have the iconview as a parent and on the iconview itself
        // e.g. the rename textedit widget when renaming a QIconViewItem
        // Qt-issue: N64698
        if ( ! pix.isNull() )
            widget->setBackgroundPixmap( pix );
        // setPaletteBackgroundPixmap leads to flicker on window activation(!)
    }

    if ( m_textColor.isValid() )
        widget->setPaletteForegroundColor( m_textColor );
}

const TQStringList& KonqPropsView::previewSettings()
{
    if ( ! d->previewsToShow )
    {
        d->previewsToShow = new TQStringList;

        if (d->previewsEnabled) {
            KTrader::OfferList plugins = KTrader::self()->query( "ThumbCreator" );
            for ( KTrader::OfferList::ConstIterator it = plugins.begin(); it != plugins.end(); ++it )
            {
            TQString name = (*it)->desktopEntryName();
            if ( ! m_dontPreview.contains(name) )
                    d->previewsToShow->append( name );
            }
            if ( ! m_dontPreview.contains( "audio/" ) )
            d->previewsToShow->append( "audio/" );
        }
    }

    return *(d->previewsToShow);
}

const TQString& KonqPropsView::sortCriterion() const {
    return d->sortcriterion;
}