1 | /* |
2 | This file is part of the kcal library. |
3 | |
4 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> |
5 | Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> |
6 | |
7 | This library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. |
11 | |
12 | This library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with this library; see the file COPYING.LIB. If not, write to |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "resourcelocalconfig.h" |
24 | #include "resourcelocal.h" |
25 | #include "resourcelocal_p.h" |
26 | #include "vcalformat.h" |
27 | #include "icalformat.h" |
28 | |
29 | #include <klocalizedstring.h> |
30 | #include <kmessagebox.h> |
31 | #include <kdebug.h> |
32 | #include <kstandarddirs.h> |
33 | |
34 | #include <QLabel> |
35 | #include <QGridLayout> |
36 | #include <QGroupBox> |
37 | |
38 | #include <typeinfo> |
39 | |
40 | |
41 | using namespace KCal; |
42 | |
43 | /** |
44 | Private class that helps to provide binary compatibility between releases. |
45 | @internal |
46 | */ |
47 | //@cond PRIVATE |
48 | class KCal::ResourceLocalConfig::Private |
49 | { |
50 | public: |
51 | Private() |
52 | {} |
53 | KUrlRequester *mURL; |
54 | QGroupBox *mFormatGroup; |
55 | QRadioButton *mIcalButton; |
56 | QRadioButton *mVcalButton; |
57 | }; |
58 | //@endcond |
59 | |
60 | ResourceLocalConfig::ResourceLocalConfig( QWidget *parent ) |
61 | : KRES::ConfigWidget( parent ), d( new KCal::ResourceLocalConfig::Private ) |
62 | { |
63 | resize( 245, 115 ); |
64 | QGridLayout *mainLayout = new QGridLayout( this ); |
65 | |
66 | QLabel *label = new QLabel( i18n( "Location:" ), this ); |
67 | d->mURL = new KUrlRequester( this ); |
68 | d->mURL->setFilter( i18n( "*.ics *.vcs|Calendar Files" ) ); |
69 | mainLayout->addWidget( label, 1, 0 ); |
70 | mainLayout->addWidget( d->mURL, 1, 1 ); |
71 | |
72 | d->mFormatGroup = new QGroupBox( i18n( "Calendar Format" ), this ); |
73 | |
74 | d->mIcalButton = new QRadioButton( i18n( "iCalendar" ), d->mFormatGroup ); |
75 | d->mVcalButton = new QRadioButton( i18n( "vCalendar" ), d->mFormatGroup ); |
76 | |
77 | QVBoxLayout *vbox = new QVBoxLayout; |
78 | vbox->addWidget( d->mIcalButton ); |
79 | vbox->addWidget( d->mVcalButton ); |
80 | vbox->addStretch( 1 ); |
81 | d->mFormatGroup->setLayout( vbox ); |
82 | |
83 | mainLayout->addWidget( d->mFormatGroup, 2, 1 ); |
84 | } |
85 | |
86 | ResourceLocalConfig::~ResourceLocalConfig() |
87 | { |
88 | delete d; |
89 | } |
90 | |
91 | void ResourceLocalConfig::loadSettings( KRES::Resource *resource ) |
92 | { |
93 | ResourceLocal* res = static_cast<ResourceLocal*>( resource ); |
94 | if ( res ) { |
95 | d->mURL->setUrl( res->d->mURL.prettyUrl() ); |
96 | kDebug() << "Format typeid().name():" << typeid( res->d->mFormat ).name(); |
97 | if ( typeid( *(res->d->mFormat) ) == typeid( ICalFormat ) ) { |
98 | d->mIcalButton->setChecked(true); |
99 | } else if ( typeid( *(res->d->mFormat) ) == typeid( VCalFormat ) ) { |
100 | d->mVcalButton->setChecked(true); |
101 | } else { |
102 | kDebug() << "ERROR: Unknown format type" ; |
103 | } |
104 | } else { |
105 | kDebug() << "ERROR: no ResourceLocal, cast failed" ; |
106 | } |
107 | } |
108 | |
109 | void ResourceLocalConfig::saveSettings( KRES::Resource *resource ) |
110 | { |
111 | KUrl url = d->mURL->url(); |
112 | |
113 | if( url.isEmpty() ) { |
114 | KStandardDirs dirs; |
115 | QString saveFolder = dirs.saveLocation( "data" , "korganizer" ); |
116 | QFile file( saveFolder + "/std.ics" ); |
117 | |
118 | // find a non-existent name |
119 | for ( int i = 0; file.exists(); ++i ) { |
120 | file.setFileName( saveFolder + "/std" + QString::number(i) + ".ics" ); |
121 | } |
122 | |
123 | KMessageBox::information( |
124 | this, |
125 | i18n( "You did not specify a URL for this resource. " |
126 | "Therefore, the resource will be saved in %1. " |
127 | "It is still possible to change this location " |
128 | "by editing the resource properties." , file.fileName() ) ); |
129 | |
130 | url = KUrl::fromPath( file.fileName() ); |
131 | } |
132 | |
133 | ResourceLocal* res = static_cast<ResourceLocal*>( resource ); |
134 | if ( res ) { |
135 | res->d->mURL = url; |
136 | |
137 | delete res->d->mFormat; |
138 | if ( d->mIcalButton->isDown() ) { |
139 | res->d->mFormat = new ICalFormat(); |
140 | } else { |
141 | res->d->mFormat = new VCalFormat(); |
142 | } |
143 | } else { |
144 | kDebug() << "ERROR: no ResourceLocal, cast failed" ; |
145 | } |
146 | } |
147 | |