1 | /* |
2 | Copyright (c) 2006 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "akonadislave.h" |
21 | |
22 | #include <akonadi/itemfetchjob.h> |
23 | #include <akonadi/itemfetchscope.h> |
24 | #include <akonadi/itemdeletejob.h> |
25 | #include <akonadi/collection.h> |
26 | #include <akonadi/collectionfetchjob.h> |
27 | #include <akonadi/collectiondeletejob.h> |
28 | #include <akonadi/entitydisplayattribute.h> |
29 | |
30 | #include <kapplication.h> |
31 | #include <kcmdlineargs.h> |
32 | #include <kdebug.h> |
33 | #include <klocale.h> |
34 | |
35 | #include <QDateTime> |
36 | |
37 | extern "C" { int KDE_EXPORT kdemain(int argc, char **argv); } |
38 | |
39 | int kdemain(int argc, char **argv) { |
40 | |
41 | KCmdLineArgs::init( argc, argv, "kio_akonadi" , 0, KLocalizedString(), 0 ); |
42 | |
43 | KCmdLineOptions options; |
44 | options.add( "+protocol" , ki18n( "Protocol name" ) ); |
45 | options.add( "+pool" , ki18n( "Socket name" ) ); |
46 | options.add( "+app" , ki18n( "Socket name" ) ); |
47 | KCmdLineArgs::addCmdLineOptions( options ); |
48 | KApplication app( false ); |
49 | |
50 | KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); |
51 | AkonadiSlave slave( args->arg( 1 ).toLocal8Bit(), args->arg( 2 ).toLocal8Bit() ); |
52 | slave.dispatchLoop(); |
53 | |
54 | return 0; |
55 | } |
56 | |
57 | using namespace Akonadi; |
58 | |
59 | AkonadiSlave::AkonadiSlave(const QByteArray & pool_socket, const QByteArray & app_socket) : |
60 | KIO::SlaveBase( "akonadi" , pool_socket, app_socket ) |
61 | { |
62 | kDebug( 7129 ) << "kio_akonadi starting up" ; |
63 | } |
64 | |
65 | AkonadiSlave::~ AkonadiSlave() |
66 | { |
67 | kDebug( 7129 ) << "kio_akonadi shutting down" ; |
68 | } |
69 | |
70 | void AkonadiSlave::get(const KUrl & url) |
71 | { |
72 | const Item item = Item::fromUrl( url ); |
73 | ItemFetchJob *job = new ItemFetchJob( item ); |
74 | job->fetchScope().fetchFullPayload(); |
75 | |
76 | if ( !job->exec() ) { |
77 | error( KIO::ERR_INTERNAL, job->errorString() ); |
78 | return; |
79 | } |
80 | |
81 | if ( job->items().count() != 1 ) { |
82 | error( KIO::ERR_DOES_NOT_EXIST, i18n( "No such item." ) ); |
83 | } else { |
84 | const Item item = job->items().first(); |
85 | QByteArray tmp = item.payloadData(); |
86 | data( tmp ); |
87 | data( QByteArray() ); |
88 | finished(); |
89 | } |
90 | |
91 | finished(); |
92 | } |
93 | |
94 | void AkonadiSlave::stat(const KUrl & url) |
95 | { |
96 | kDebug( 7129 ) << url; |
97 | |
98 | // Stats for a collection |
99 | if ( Collection::fromUrl( url ).isValid() ) { |
100 | Collection collection = Collection::fromUrl( url ); |
101 | |
102 | if ( collection != Collection::root() ) { |
103 | // Check that the collection exists. |
104 | CollectionFetchJob *job = new CollectionFetchJob( collection, CollectionFetchJob::Base ); |
105 | if ( !job->exec() ) { |
106 | error( KIO::ERR_INTERNAL, job->errorString() ); |
107 | return; |
108 | } |
109 | |
110 | if ( job->collections().count() != 1 ) { |
111 | error( KIO::ERR_DOES_NOT_EXIST, i18n( "No such item." ) ); |
112 | return; |
113 | } |
114 | |
115 | collection = job->collections().first(); |
116 | } |
117 | |
118 | statEntry( entryForCollection( collection ) ); |
119 | finished(); |
120 | } |
121 | // Stats for an item |
122 | else if ( Item::fromUrl( url ).isValid() ) { |
123 | ItemFetchJob *job = new ItemFetchJob( Item::fromUrl( url ) ); |
124 | |
125 | if ( !job->exec() ) { |
126 | error( KIO::ERR_INTERNAL, job->errorString() ); |
127 | return; |
128 | } |
129 | |
130 | if ( job->items().count() != 1 ) { |
131 | error( KIO::ERR_DOES_NOT_EXIST, i18n( "No such item." ) ); |
132 | return; |
133 | } |
134 | |
135 | const Item item = job->items().first(); |
136 | statEntry( entryForItem( item ) ); |
137 | finished(); |
138 | } |
139 | } |
140 | |
141 | void AkonadiSlave::del( const KUrl &url, bool isFile ) |
142 | { |
143 | kDebug( 7129 ) << url; |
144 | |
145 | if ( !isFile ) { // It's a directory |
146 | Collection collection = Collection::fromUrl( url ); |
147 | CollectionDeleteJob *job = new CollectionDeleteJob( collection ); |
148 | if ( !job->exec() ) { |
149 | error( KIO::ERR_INTERNAL, job->errorString() ); |
150 | return; |
151 | } |
152 | finished(); |
153 | } else { // It's a file |
154 | ItemDeleteJob* job = new ItemDeleteJob( Item::fromUrl( url ) ); |
155 | if ( !job->exec() ) { |
156 | error( KIO::ERR_INTERNAL, job->errorString() ); |
157 | return; |
158 | } |
159 | finished(); |
160 | } |
161 | } |
162 | |
163 | void AkonadiSlave::listDir( const KUrl &url ) |
164 | { |
165 | kDebug( 7129 ) << url; |
166 | |
167 | if ( !Collection::fromUrl( url ).isValid() ) { |
168 | error( KIO::ERR_DOES_NOT_EXIST, i18n( "No such collection." ) ); |
169 | return; |
170 | } |
171 | |
172 | // Fetching collections |
173 | Collection collection = Collection::fromUrl( url ); |
174 | if ( !collection.isValid() ) { |
175 | error( KIO::ERR_DOES_NOT_EXIST, i18n( "No such collection." ) ); |
176 | return; |
177 | } |
178 | CollectionFetchJob *job = new CollectionFetchJob( collection, CollectionFetchJob::FirstLevel ); |
179 | if ( !job->exec() ) { |
180 | error( KIO::ERR_CANNOT_ENTER_DIRECTORY, job->errorString() ); |
181 | return; |
182 | } |
183 | |
184 | Collection::List collections = job->collections(); |
185 | foreach ( const Collection &col, collections ) |
186 | listEntry( entryForCollection( col ), false ); |
187 | |
188 | // Fetching items |
189 | if ( collection != Collection::root() ) { |
190 | ItemFetchJob* fjob = new ItemFetchJob( collection ); |
191 | if ( !fjob->exec() ) { |
192 | error( KIO::ERR_INTERNAL, job->errorString() ); |
193 | return; |
194 | } |
195 | Item::List items = fjob->items(); |
196 | totalSize( collections.count() + items.count() ); |
197 | foreach ( const Item &item, items ) |
198 | listEntry( entryForItem( item ), false ); |
199 | } |
200 | |
201 | listEntry( KIO::UDSEntry(), true ); |
202 | finished(); |
203 | } |
204 | |
205 | KIO::UDSEntry AkonadiSlave::entryForItem(const Akonadi::Item& item) |
206 | { |
207 | KIO::UDSEntry entry; |
208 | entry.insert( KIO::UDSEntry::UDS_NAME, QString::number( item.id() ) ); |
209 | entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, item.mimeType() ); |
210 | entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFREG ); |
211 | entry.insert( KIO::UDSEntry::UDS_URL, item.url().url() ); |
212 | entry.insert( KIO::UDSEntry::UDS_SIZE, item.size() ); |
213 | entry.insert( KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IRGRP | S_IROTH ); |
214 | entry.insert( KIO::UDSEntry::UDS_MODIFICATION_TIME, item.modificationTime().toTime_t() ); |
215 | return entry; |
216 | } |
217 | |
218 | KIO::UDSEntry AkonadiSlave::entryForCollection(const Akonadi::Collection& collection) |
219 | { |
220 | KIO::UDSEntry entry; |
221 | entry.insert( KIO::UDSEntry::UDS_NAME, collection.name() ); |
222 | entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, Collection::mimeType() ); |
223 | entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR ); |
224 | entry.insert( KIO::UDSEntry::UDS_URL, collection.url().url() ); |
225 | entry.insert( KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IRGRP | S_IROTH ); |
226 | if ( EntityDisplayAttribute *attr = collection.attribute<EntityDisplayAttribute>() ) { |
227 | if ( !attr->iconName().isEmpty() ) |
228 | entry.insert( KIO::UDSEntry::UDS_ICON_NAME, attr->iconName() ); |
229 | if ( !attr->displayName().isEmpty() ) |
230 | entry.insert( KIO::UDSEntry::UDS_DISPLAY_NAME, attr->displayName() ); |
231 | } |
232 | return entry; |
233 | } |
234 | |