korganizer

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 Qt, and distribute the resulting executable,
00026     without including the source code for Qt 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 <qlabel.h>
00045 #include <qlayout.h>
00046 #include <qradiobutton.h>
00047 #include <qpushbutton.h>
00048 #include <qdialog.h>
00049 
00050 using namespace KCal;
00051 
00052 PreviewDialog::PreviewDialog( const KURL &url, QWidget *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   QFrame *topFrame = plainPage();
00058   QVBoxLayout *topLayout = new QVBoxLayout( 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, SIGNAL(user1Clicked()), SLOT(slotMerge()) );
00068   connect( this, SIGNAL(user2Clicked()), 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     QString 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 QString fileName = KFileDialog::getSaveFileName( locateLocal( "data","korganizer/" ),
00139                                                            i18n( "*.vcs *.ics|Calendar Files" ),
00140                                                            0, i18n( "Select path for new calendar" ) );
00141     finalUrl = KURL( fileName );
00142 
00143     KIO::NetAccess::copy( mOriginalUrl, finalUrl, this );
00144   }
00145 
00146   if ( finalUrl.isValid() ) {
00147     emit addResource( finalUrl );
00148     emit dialogFinished( this );
00149     accept();
00150   }
00151 }
00152 
00153 bool PreviewDialog::isTempFile() const
00154 {
00155   return mOriginalUrl.path().startsWith( locateLocal( "tmp", "" ) );
00156 }
00157 
00158 #include "previewdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys