korganizer

previewdialog.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7  Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
8  Author: Sergio Martins, <sergio.martins@kdab.com>
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 
24  As a special exception, permission is given to link this program
25  with any edition of TQt, and distribute the resulting executable,
26  without including the source code for TQt in the source distribution.
27 */
28 
29 #include "previewdialog.h"
30 
31 #include "kolistview.h"
32 #include "koprefs.h"
33 #include "stdcalendar.h"
34 
35 #include <tdelocale.h>
36 
37 #include <libkcal/calendarlocal.h>
38 
39 #include <kstandarddirs.h>
40 #include <tdefiledialog.h>
41 #include <tdemessagebox.h>
42 #include <tdeio/netaccess.h>
43 
44 #include <tqlabel.h>
45 #include <tqlayout.h>
46 #include <tqradiobutton.h>
47 #include <tqpushbutton.h>
48 #include <tqdialog.h>
49 
50 using namespace KCal;
51 
52 PreviewDialog::PreviewDialog( const KURL &url, TQWidget *parent )
53  : KDialogBase( Plain, i18n("Import Calendar/Event"), User1 | User2 | Cancel, User1, parent,
54  0, true, true, KGuiItem( i18n("&Merge into existing calendar"), "merge" ) ),
55  mOriginalUrl( url )
56 {
57  TQFrame *topFrame = plainPage();
58  TQVBoxLayout *topLayout = new TQVBoxLayout( topFrame, 0, spacingHint() );
59 
60  mCalendar = new CalendarLocal( KOPrefs::instance()->mTimeZoneId );
61  mListView = new KOListView( mCalendar, topFrame, "PreviewDialog::ListView", true );
62  topLayout->addWidget( mListView );
63 
64  topLayout->setSpacing( spacingHint() );
65  topLayout->setMargin( marginHint() );
66 
67  connect( this, TQ_SIGNAL(user1Clicked()), TQ_SLOT(slotMerge()) );
68  connect( this, TQ_SIGNAL(user2Clicked()), TQ_SLOT(slotAdd()) );
69 
70  // when someone edits a kmail attachment he's editing a tmp file, check for that
71  // and if it's a tmp file then open a save dialog
72  if ( isTempFile() ) {
73  setButtonGuiItem( User2, KGuiItem( i18n("&Add as new calendar..."), "add" ) );
74  } else {
75  setButtonGuiItem( User2, KGuiItem( i18n("&Add as new calendar"), "add" ) );
76  }
77 
78  mLocalUrl = 0;
79 }
80 
81 PreviewDialog::~PreviewDialog()
82 {
83  if ( mLocalUrl && !mOriginalUrl.isLocalFile() ) {
84  TDEIO::NetAccess::removeTempFile( mLocalUrl->path() );
85  delete mLocalUrl;
86  }
87 
88  delete mCalendar;
89 }
90 
91 bool PreviewDialog::loadCalendar()
92 {
93  // If it's a remote file, download it so we can give it to CalendarLocal
94  if ( !mOriginalUrl.isLocalFile() ) {
95  if ( mLocalUrl ) {
96  // loadCalendar already called.. remove old one.
97  TDEIO::NetAccess::removeTempFile( mLocalUrl->path() );
98  delete mLocalUrl;
99  }
100 
101  TQString tmpFile;
102  if ( TDEIO::NetAccess::download( mOriginalUrl, tmpFile, 0 ) ) {
103  mLocalUrl = new KURL( tmpFile );
104  } else {
105  mLocalUrl = 0;
106  }
107  } else {
108  mLocalUrl = &mOriginalUrl;
109  }
110 
111  if ( mLocalUrl ) {
112  const bool success = mCalendar->load( mLocalUrl->path() );
113 
114  if ( !success && !mOriginalUrl.isLocalFile() ) {
115  TDEIO::NetAccess::removeTempFile( mLocalUrl->path() );
116  } else {
117  mListView->showAll();
118  }
119  return success;
120  } else {
121  return false;
122  }
123 }
124 
125 void PreviewDialog::slotMerge()
126 {
127  if ( mLocalUrl ) {
128  emit openURL( *mLocalUrl, true );
129  emit dialogFinished( this );
130  accept();
131  }
132 }
133 
134 void PreviewDialog::slotAdd()
135 {
136  KURL finalUrl = mOriginalUrl;
137  if ( isTempFile() ) {
138  const TQString fileName =
139  KFileDialog::getSaveFileName( locateLocal( "data","korganizer/" ),
140  i18n( "*.vcs *.ics|Calendar Files" ),
141  this, i18n( "Select path for new calendar" ) );
142 
143  finalUrl = KURL( fileName );
144 
145  if ( !TDEIO::NetAccess::copy( mOriginalUrl, finalUrl, this ) && TDEIO::NetAccess::lastError() ) {
146  KMessageBox::error( this, TDEIO::NetAccess::lastErrorString() );
147  return;
148  }
149  }
150 
151  if ( finalUrl.isValid() ) {
152  emit addResource( finalUrl );
153  emit dialogFinished( this );
154  accept();
155  }
156 }
157 
158 bool PreviewDialog::isTempFile() const
159 {
160  return mOriginalUrl.path().startsWith( locateLocal( "tmp", "" ) );
161 }
162 
163 #include "previewdialog.moc"
This class provides a multi-column list view of events.
Definition: kolistview.h:69