summaryrefslogtreecommitdiffstats
path: root/kmymoney2/dialogs/kstartdlg.cpp
blob: 3c08aaac612a1f8e8d8437902e7bc64579d5bd34 (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
/***************************************************************************
                          KStartDlg.cpp  -  description
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// ----------------------------------------------------------------------------
// TQt Includes

#include <tqvbox.h>
#include <tqlayout.h>
#include <tqbuttongroup.h>
#include <tqabstractlayout.h>
#include <tqpixmap.h>
#include <tqtextview.h>
#include <tqlabel.h>

// ----------------------------------------------------------------------------
// TDE Includes

#include <tdeversion.h>

#include <kstandarddirs.h>

#include <kiconloader.h>
#include <tdeconfig.h>
#include <tdeglobalsettings.h>
#include <tdefiledialog.h>
#include <kurl.h>
#include <kurlrequester.h>
#include <tdefile.h>
#include <tdeio/netaccess.h>
#include <tdemessagebox.h>

// ----------------------------------------------------------------------------
// Project Includes

#include "kstartdlg.h"
#include "tderecentfileitem.h"
#include "../kmymoney2.h"

#include <tqtooltip.h>

KStartDlg::KStartDlg(TQWidget *parent, const char *name, bool modal) : KDialogBase(IconList,i18n("Start Dialog"),Help|Ok|Cancel,Ok, parent, name, modal, true)
{
  setPage_Template();
  setPage_Documents();

  isnewfile = false;
  isopenfile = false;

  readConfig();
}

KStartDlg::~KStartDlg()
{
}

/** Set the font  Page of the preferences dialog */
void KStartDlg::setPage_Template()
{
  TDEIconLoader *ic = TDEGlobal::iconLoader();
  templateMainFrame = addVBoxPage( i18n("Templates"), i18n("Select templates"), DesktopIcon("wizard"));
  view_wizard = new TDEIconView( templateMainFrame, "view_options" );
  (void)new TQIconViewItem( view_wizard, i18n("New KMyMoney document"), ic->loadIcon("mime_empty.png", TDEIcon::Desktop, TDEIcon::SizeLarge)/*TQPixmap( locate("icon","hicolor/48x48/mimetypes/mime_empty.png") )*/ );
  connect(view_wizard, TQ_SIGNAL(executed(TQIconViewItem *) ), this, TQ_SLOT(slotTemplateClicked(TQIconViewItem *) ) );
  connect(view_wizard, TQ_SIGNAL(selectionChanged(TQIconViewItem*)),
    this, TQ_SLOT(slotTemplateSelectionChanged(TQIconViewItem*)));
  connect(this, TQ_SIGNAL(aboutToShowPage(TQWidget*)), this, TQ_SLOT(slotAboutToShowPage(TQWidget*)));
}

/** Set the Misc options Page of the preferences dialog */
void KStartDlg::setPage_Documents()
{
  recentMainFrame = addPage( i18n("Open"), i18n("Open a KMyMoney document"), DesktopIcon("document-open"));
  TQVBoxLayout *mainLayout = new TQVBoxLayout( recentMainFrame );

  kurlrequest = new KURLRequester( recentMainFrame, "kurlrequest" );

  //allow user to select either a .kmy file, or any generic file.
  kurlrequest->fileDialog()->setFilter( i18n("%1|KMyMoney files (*.kmy)\n" "%2|All files (*.*)").arg("*.kmy").arg("*.*") );
  kurlrequest->fileDialog()->setMode(KFile::File || KFile::ExistingOnly);
  kurlrequest->fileDialog()->setURL(KURL(kmymoney2->readLastUsedDir()));//kurlrequest->fileDialog()->setURL(KURL(TDEGlobalSettings::documentPath()));
  mainLayout->addWidget( kurlrequest );

  TQLabel *label1 = new TQLabel( recentMainFrame, "label1" );
  label1->setText( i18n("Recent Files") );
  mainLayout->addWidget( label1 );
  view_recent = new TDEIconView( recentMainFrame, "view_recent" );
  connect( view_recent, TQ_SIGNAL( executed(TQIconViewItem *) ), this, TQ_SLOT( slotRecentClicked(TQIconViewItem *) ) );
  mainLayout->addWidget( view_recent );
  view_recent->setArrangement(TDEIconView::LeftToRight/*TopToBottom*/);
  view_recent->setItemTextPos(TDEIconView::Bottom);

  connect(view_recent, TQ_SIGNAL(selectionChanged(TQIconViewItem*)),
    this, TQ_SLOT(slotRecentSelectionChanged(TQIconViewItem*)));
}

void KStartDlg::slotTemplateClicked(TQIconViewItem *item)
{
  if(!item) return;

  // If the item is the blank document turn isnewfile variable true, else is template or wizard
  if( item->text() == i18n("New KMyMoney document") )
     isnewfile = true;
   else
     templatename = item->text();

  isopenfile = false;
  // Close the window if the user pressed an icon
  slotOk();
}

/** Read config window */
void KStartDlg::readConfig()
{
  TQString value;
  unsigned int i = 1;

  TDEConfig *config = TDEGlobal::config();
  TDEIconLoader *il = TDEGlobal::iconLoader();

  // read file list
  do {
    // for some reason, I had to setup the group to get reasonable results
    // after program startup. If the wizard was opened the second time,
    // it does not make a difference, if you call setGroup() outside of
    // this loop. The first time it does make a difference!
    config->setGroup("Recent Files");
    value = config->readEntry( TQString( "File%1" ).arg( i ), TQString() );
    if( !value.isNull() && fileExists(value) )
    {
      TQString file_name = value.mid(value.findRev('/')+1);
      (void)new TDERecentFileItem( value, view_recent, file_name, il->loadIcon("kmy", TDEIcon::Desktop, TDEIcon::SizeLarge));
    }
    i++;
  } while( !value.isNull() );

  config->setGroup("Start Dialog");
  TQSize *defaultSize = new TQSize(400,300);
  this->resize( config->readSizeEntry("Geometry", defaultSize ) );

  // Restore the last page viewed
  // default to the recent files page if no entry exists but files have been found
  // otherwise, default to template page
  if(view_recent->count() > 0)
    showPage(config->readNumEntry("LastPage", this->pageIndex(recentMainFrame)));
  else {
    showPage(config->readNumEntry("LastPage", this->pageIndex(templateMainFrame)));
    slotAboutToShowPage(templateMainFrame);
  }
}

/** Write config window */
void KStartDlg::writeConfig()
{
  TDEConfig *config = TDEGlobal::config();

  config->setGroup("Start Dialog");
  config->writeEntry("Geometry", this->size() );
  config->writeEntry("LastPage", this->activePageIndex());
  config->sync();
}

/** slot to recent view */
void KStartDlg::slotRecentClicked(TQIconViewItem *item)
{
  TDERecentFileItem *kitem = (TDERecentFileItem*)item;
  if(!kitem) return;

  isopenfile = true;
  kurlrequest->setURL( kitem->fileURL() );
  // Close the window if the user press an icon
  slotOk();
}

/** No descriptions */
void KStartDlg::slotOk()
{
  writeConfig();
  this->accept();
}

bool KStartDlg::fileExists(KURL url)
{
#if KDE_IS_VERSION(3,2,0)
  return TDEIO::NetAccess::exists(url, true, this);
#else
  return TDEIO::NetAccess::exists(url);
#endif
}

void KStartDlg::slotTemplateSelectionChanged(TQIconViewItem* item)
{
  if(!item) return;

  // Clear the other selection
  view_recent->clearSelection();

  // If the item is the blank document turn isnewfile
  // variable true, else is template or wizard
  if( item->text() == i18n("Blank Document") )
    isnewfile = true;
  else
    templatename = item->text();

  isopenfile = false;
}

void KStartDlg::slotRecentSelectionChanged(TQIconViewItem* item)
{
  TDERecentFileItem *kitem = (TDERecentFileItem*)item;
  if(!kitem) return;

  // Clear the other selection
  view_wizard->clearSelection();

  isnewfile = false;
  isopenfile = true;
  kurlrequest->setURL( kitem->fileURL() );
}

void KStartDlg::slotAboutToShowPage(TQWidget* page)
{
  enableButtonOK(page == recentMainFrame);
}

#include "kstartdlg.moc"