previewdialog.cpp
00001 /* 00002 This file is part of KOrganizer. 00003 00004 Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org> 00005 Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com> 00006 00007 Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net> 00008 Author: Sergio Martins, <sergio.martins@kdab.com> 00009 00010 This program is free software; you can redistribute it and/or modify 00011 it under the terms of the GNU General Public License as published by 00012 the Free Software Foundation; either version 2 of the License, or 00013 (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program; if not, write to the Free Software 00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00023 00024 As a special exception, permission is given to link this program 00025 with any edition of TQt, and distribute the resulting executable, 00026 without including the source code for TQt in the source distribution. 00027 */ 00028 00029 #include "previewdialog.h" 00030 00031 #include "kolistview.h" 00032 #include "koprefs.h" 00033 #include "stdcalendar.h" 00034 00035 #include <klocale.h> 00036 00037 #include <libkcal/calendarlocal.h> 00038 00039 #include <kstandarddirs.h> 00040 #include <kfiledialog.h> 00041 #include <kmessagebox.h> 00042 #include <kio/netaccess.h> 00043 00044 #include <tqlabel.h> 00045 #include <tqlayout.h> 00046 #include <tqradiobutton.h> 00047 #include <tqpushbutton.h> 00048 #include <tqdialog.h> 00049 00050 using namespace KCal; 00051 00052 PreviewDialog::PreviewDialog( const KURL &url, TQWidget *parent ) 00053 : KDialogBase( Plain, i18n("Import Calendar/Event"), User1 | User2 | Cancel, User1, parent, 00054 0, true, true, KGuiItem( i18n("&Merge into existing calendar"), "merge" ) ), 00055 mOriginalUrl( url ) 00056 { 00057 TQFrame *topFrame = plainPage(); 00058 TQVBoxLayout *topLayout = new TQVBoxLayout( topFrame, 0, spacingHint() ); 00059 00060 mCalendar = new CalendarLocal( KOPrefs::instance()->mTimeZoneId ); 00061 mListView = new KOListView( mCalendar, topFrame, "PreviewDialog::ListView", true ); 00062 topLayout->addWidget( mListView ); 00063 00064 topLayout->setSpacing( spacingHint() ); 00065 topLayout->setMargin( marginHint() ); 00066 00067 connect( this, TQT_SIGNAL(user1Clicked()), TQT_SLOT(slotMerge()) ); 00068 connect( this, TQT_SIGNAL(user2Clicked()), TQT_SLOT(slotAdd()) ); 00069 00070 // when someone edits a kmail attachment he's editing a tmp file, check for that 00071 // and if it's a tmp file then open a save dialog 00072 if ( isTempFile() ) { 00073 setButtonGuiItem( User2, KGuiItem( i18n("&Add as new calendar..."), "add" ) ); 00074 } else { 00075 setButtonGuiItem( User2, KGuiItem( i18n("&Add as new calendar"), "add" ) ); 00076 } 00077 00078 mLocalUrl = 0; 00079 } 00080 00081 PreviewDialog::~PreviewDialog() 00082 { 00083 if ( mLocalUrl && !mOriginalUrl.isLocalFile() ) { 00084 KIO::NetAccess::removeTempFile( mLocalUrl->path() ); 00085 delete mLocalUrl; 00086 } 00087 00088 delete mCalendar; 00089 } 00090 00091 bool PreviewDialog::loadCalendar() 00092 { 00093 // If it's a remote file, download it so we can give it to CalendarLocal 00094 if ( !mOriginalUrl.isLocalFile() ) { 00095 if ( mLocalUrl ) { 00096 // loadCalendar already called.. remove old one. 00097 KIO::NetAccess::removeTempFile( mLocalUrl->path() ); 00098 delete mLocalUrl; 00099 } 00100 00101 TQString tmpFile; 00102 if ( KIO::NetAccess::download( mOriginalUrl, tmpFile, 0 ) ) { 00103 mLocalUrl = new KURL( tmpFile ); 00104 } else { 00105 mLocalUrl = 0; 00106 } 00107 } else { 00108 mLocalUrl = &mOriginalUrl; 00109 } 00110 00111 if ( mLocalUrl ) { 00112 const bool success = mCalendar->load( mLocalUrl->path() ); 00113 00114 if ( !success && !mOriginalUrl.isLocalFile() ) { 00115 KIO::NetAccess::removeTempFile( mLocalUrl->path() ); 00116 } else { 00117 mListView->showAll(); 00118 } 00119 return success; 00120 } else { 00121 return false; 00122 } 00123 } 00124 00125 void PreviewDialog::slotMerge() 00126 { 00127 if ( mLocalUrl ) { 00128 emit openURL( *mLocalUrl, true ); 00129 emit dialogFinished( this ); 00130 accept(); 00131 } 00132 } 00133 00134 void PreviewDialog::slotAdd() 00135 { 00136 KURL finalUrl = mOriginalUrl; 00137 if ( isTempFile() ) { 00138 const TQString fileName = 00139 KFileDialog::getSaveFileName( locateLocal( "data","korganizer/" ), 00140 i18n( "*.vcs *.ics|Calendar Files" ), 00141 this, i18n( "Select path for new calendar" ) ); 00142 00143 finalUrl = KURL( fileName ); 00144 00145 if ( !KIO::NetAccess::copy( mOriginalUrl, finalUrl, this ) && KIO::NetAccess::lastError() ) { 00146 KMessageBox::error( this, KIO::NetAccess::lastErrorString() ); 00147 return; 00148 } 00149 } 00150 00151 if ( finalUrl.isValid() ) { 00152 emit addResource( finalUrl ); 00153 emit dialogFinished( this ); 00154 accept(); 00155 } 00156 } 00157 00158 bool PreviewDialog::isTempFile() const 00159 { 00160 return mOriginalUrl.path().startsWith( locateLocal( "tmp", "" ) ); 00161 } 00162 00163 #include "previewdialog.moc"