summaryrefslogtreecommitdiffstats
path: root/src/k3btempdirselectionwidget.cpp
blob: 22b2750902e0192f81dfa910bc2b79b2de88fa9c (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
/*
 *
 * $Id: k3btempdirselectionwidget.cpp 690207 2007-07-20 10:40:19Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <[email protected]>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <[email protected]>
 *
 * 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.
 * See the file "COPYING" for the exact licensing terms.
 */


#include "k3btempdirselectionwidget.h"
#include <k3bglobals.h>
#include <k3bcore.h>

#include <tqlabel.h>
#include <tqgroupbox.h>
#include <tqlayout.h>
#include <tqtimer.h>
#include <tqhbox.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>
#include <tqfileinfo.h>

#include <tdeconfig.h>
#include <klocale.h>
#include <tdefiledialog.h>
#include <kdialog.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kurlrequester.h>
#include <tdeio/global.h>
#include <tdeconfig.h>
#include <klineedit.h>


K3bTempDirSelectionWidget::K3bTempDirSelectionWidget( TQWidget *parent, const char *name )
  : TQGroupBox( 4, Qt::Vertical, parent, name ),
    m_labelCdSize(0),
    m_defaultImageFileName( "k3b_image.iso" )
{
  layout()->setSpacing( KDialog::spacingHint() );
  layout()->setMargin( KDialog::marginHint() );

  m_imageFileLabel = new TQLabel( this );
  m_editDirectory = new KURLRequester( this, "m_editDirectory" );

  m_imageFileLabel->setBuddy( m_editDirectory );

  TQHBox* freeTempSpaceBox = new TQHBox( this );
  freeTempSpaceBox->setSpacing( KDialog::spacingHint() );
  (void)new TQLabel( i18n( "Free space in temporary directory:" ), freeTempSpaceBox, "TextLabel2" );
  m_labelFreeSpace = new TQLabel( "                       ",freeTempSpaceBox, "m_labelFreeSpace" );
  m_labelFreeSpace->setAlignment( int( TQLabel::AlignVCenter | TQLabel::AlignRight ) );


  connect( m_editDirectory, TQT_SIGNAL(openFileDialog(KURLRequester*)),
	   this, TQT_SLOT(slotTempDirButtonPressed(KURLRequester*)) );
  connect( m_editDirectory, TQT_SIGNAL(textChanged(const TQString&)),
	   this, TQT_SLOT(slotUpdateFreeTempSpace()) );
  connect( m_editDirectory->lineEdit(), TQT_SIGNAL(lostFocus()),
           this, TQT_SLOT(slotFixTempPath()) );

  // choose a default
  setSelectionMode( DIR );

  m_editDirectory->setURL( K3b::defaultTempPath() );
  slotUpdateFreeTempSpace();

  // ToolTips
  // --------------------------------------------------------------------------------
  TQToolTip::add( m_editDirectory, i18n("The directory in which to save the image files") );

  // What's This info
  // --------------------------------------------------------------------------------
  TQWhatsThis::add( m_editDirectory, i18n("<p>This is the directory in which K3b will save the <em>image files</em>."
					 "<p>Please make sure that it resides on a partition that has enough free space.") );
}


K3bTempDirSelectionWidget::~K3bTempDirSelectionWidget()
{
}


unsigned long K3bTempDirSelectionWidget::freeTempSpace() const
{
  TQString path = m_editDirectory->url();

  if( !TQFile::exists( path ) )
    path.truncate( path.findRev('/') );

  unsigned long size;
  K3b::kbFreeOnFs( path, size, m_freeTempSpace );

  return m_freeTempSpace;
}


void K3bTempDirSelectionWidget::slotUpdateFreeTempSpace()
{
  // update the temp space
  freeTempSpace();

  m_labelFreeSpace->setText( TDEIO::convertSizeFromKB(m_freeTempSpace) );

  if( m_labelCdSize ) {
    if( m_freeTempSpace < m_requestedSize/1024 )
      m_labelCdSize->setPaletteForegroundColor( red );
    else
      m_labelCdSize->setPaletteForegroundColor( m_labelFreeSpace->paletteForegroundColor() );
  }
  TQTimer::singleShot( 1000, this, TQT_SLOT(slotUpdateFreeTempSpace()) );
}


void K3bTempDirSelectionWidget::slotTempDirButtonPressed( KURLRequester* r )
{
  // set the correct mode for the filedialog
  if( m_mode == DIR ) {
    r->setCaption( i18n("Select Temporary Directory") );
    r->setMode( KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly );
  }
  else {
    r->setCaption( i18n("Select Temporary File") );
    r->setMode( KFile::File | KFile::LocalOnly );
  }
}


void K3bTempDirSelectionWidget::setTempPath( const TQString& dir )
{
  m_editDirectory->setURL( dir );
  slotUpdateFreeTempSpace();
}


TQString K3bTempDirSelectionWidget::tempPath() const
{
  TQFileInfo fi( m_editDirectory->url() );

  if( fi.exists() ) {
    if( m_mode == DIR ) {
      if( fi.isDir() )
	return fi.absFilePath();
      else
	return fi.dirPath( true );
    }
    else {
      if( fi.isFile() )
	return fi.absFilePath();
      else
	return fi.absFilePath() + "/k3b_image.iso";
    }
  }
  else {
    return fi.absFilePath();
  }
}


TQString K3bTempDirSelectionWidget::plainTempPath() const
{
  return m_editDirectory->url();
}


TQString K3bTempDirSelectionWidget::tempDirectory() const
{
  TQString td( m_editDirectory->url() );

  // remove a trailing slash
  while( !td.isEmpty() && td[td.length()-1] == '/' )
    td.truncate( td.length()-1 );

  TQFileInfo fi( td );
  if( fi.exists() && fi.isDir() )
    return td + "/";

  // now we treat the last section as a filename and return the path
  // in front of it
  td.truncate( td.findRev( '/' ) + 1 );
  return td;
}


void K3bTempDirSelectionWidget::setSelectionMode( int mode )
{
  m_mode = mode;

  if( m_mode == DIR ) {
    m_imageFileLabel->setText( i18n( "Wri&te image files to:" ) );
    setTitle( i18n("Temporary Directory") );
  }
  else {
    m_imageFileLabel->setText( i18n( "Wri&te image file to:" ) );
    setTitle( i18n("Temporary File") );
  }
}


void K3bTempDirSelectionWidget::setNeededSize( TDEIO::filesize_t bytes )
{
  m_requestedSize = bytes;
  if( !m_labelCdSize ) {
    TQHBox* cdSizeBox = new TQHBox( this );
    cdSizeBox->setSpacing( KDialog::spacingHint() );
    (void)new TQLabel( i18n( "Size of project:" ), cdSizeBox, "TextLabel4" );
    m_labelCdSize = new TQLabel( TDEIO::convertSize(bytes), cdSizeBox, "m_labelCdSize" );
    m_labelCdSize->setAlignment( int( TQLabel::AlignVCenter | TQLabel::AlignRight ) );
  }
  m_labelCdSize->setText( TDEIO::convertSize(bytes) );
}


void K3bTempDirSelectionWidget::saveConfig()
{
  TDEConfigGroup grp( k3bcore->config(), "General Options" );
  grp.writePathEntry( "Temp Dir", tempDirectory() );
}


void K3bTempDirSelectionWidget::readConfig( TDEConfigBase* c )
{
  setTempPath( c->readPathEntry( "image path", K3b::defaultTempPath() ) );
}


void K3bTempDirSelectionWidget::saveConfig( TDEConfigBase* c )
{
  c->writePathEntry( "image path", tempPath() );
}


void K3bTempDirSelectionWidget::setDefaultImageFileName( const TQString& name )
{
    if ( !name.isEmpty() ) {
        bool changeImageName = false;
        if ( selectionMode() == FILE ) {
            if ( plainTempPath().section( '/', -1 ) == m_defaultImageFileName ) {
                changeImageName = true;
            }
        }

        m_defaultImageFileName = name;
        if ( !m_defaultImageFileName.contains( '.' ) ) {
            m_defaultImageFileName += ".iso";
        }
        fixTempPath( changeImageName );
    }
}


void K3bTempDirSelectionWidget::slotFixTempPath()
{
    fixTempPath( false );
}


void K3bTempDirSelectionWidget::fixTempPath( bool forceNewImageName )
{
    // if in file selection mode and no image file is specified or
    // forceNewImageName is true set the default image file name
    if ( selectionMode() == FILE ) {
        if ( forceNewImageName ||
             TQFileInfo( plainTempPath() ).isDir() ) {
            setTempPath( tempDirectory() + m_defaultImageFileName );
        }
    }
}

#include "k3btempdirselectionwidget.moc"