• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kmobiletools

p2kimpl.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (c) 2004 Kevin Ottens <ervin ipsquad net>
00003    Rearranged by Marco Gulino <marco@kmobiletools.org> for the mobile protocol
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to the
00017    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "p2kimpl.h"
00022 
00023 #include <kdebug.h>
00024 #include <kglobalsettings.h>
00025 #include <kstandarddirs.h>
00026 #include <kdesktopfile.h>
00027 
00028 #include <qapplication.h>
00029 #include <qeventloop.h>
00030 #include <qdir.h>
00031 //Added by qt3to4:
00032 #include <Q3ValueList>
00033 #include <kmimetype.h>
00034 #include <qregexp.h>
00035 #include <sys/stat.h>
00036 
00037 #include "kmobiletools_cfg.h"
00038 #include "devicesconfig.h"
00039 
00040 
00041 #include "p2kwrapper.h"
00042 
00043 P2KImpl::P2KImpl() : QObject()
00044 {
00045     KGlobal::dirs()->addResourceType("p2k_entries",
00046         KStandardDirs::kde_default("data") + "p2kview");
00049  p2kWrapper=new P2KWrapper();
00050 }
00051 
00052 bool P2KImpl::listRoot(const KUrl &url, Q3ValueList<KIO::UDSEntry> &list)
00053 {
00054     kDebug() <<"P2KImpl::listRoot";
00055     if(p2kWrapper->connectPhone() ) kDebug() <<"Files count:" << p2kWrapper->countFiles();
00056     QStringList names_found;
00057     QStringList dirList = p2kWrapper->getRoot();
00058     kDebug() <<"P2KImpl::" << dirList;
00059     QStringList::ConstIterator dirpath = dirList.begin();
00060     QStringList::ConstIterator end = dirList.end();
00061     for(; dirpath!=end; ++dirpath)
00062     {
00063         KIO::UDSEntry entry;
00064         entry.clear();
00065         createEntry( entry, url.url(-1), QString(*dirpath).replace("//", "/"), true );
00066         list.append(entry);
00067         names_found.append(*dirpath);
00068     }
00069 
00070     return true;
00071 }
00072 
00073 bool P2KImpl::listDirectory(const KUrl &url, Q3ValueList<KIO::UDSEntry> &list)
00074 {
00075     if (!p2kWrapper->getFiles().count() ) p2kWrapper->fetchFileList();
00076     kDebug() <<"P2KImpl::listDirectory:" << url;
00077     kDebug() <<"P2KImpl::listDirectory---- directory path only:" << url.path();
00078     QStringList names_found;
00079     QStringList dirList = p2kWrapper->getDirs();
00080 //     dirList << "/b" << "/b/prova";
00081 //     kDebug() <<"Directory list:" << dirList;
00082     QRegExp regexp;
00083     regexp.setPattern(QString("^").append(QRegExp::escape(url.path()) ) );
00084     names_found=dirList.grep(regexp);
00085     QString foundstr;
00086     for(QStringList::Iterator it=names_found.begin(); it!=names_found.end(); ++it)
00087     {
00088         if((*it)==url.path()) continue;
00089         foundstr=(*it).replace(regexp, "");
00090         if(foundstr[foundstr.length()-1]==QDir::separator()) foundstr=foundstr.left(foundstr.length()-2);
00091         if(foundstr[0]==QDir::separator()) foundstr=foundstr.mid(1);
00092 //         kDebug() <<"P2KImpl Listing directory `" << foundstr <<"` in" << url.path();
00093         if(foundstr.contains(QDir::separator() )) continue;
00094 //         foundstr=foundstr.remove(url.path());
00095         KIO::UDSEntry entry;
00096         createEntry( entry, url.url(), foundstr );
00097         list.append(entry);
00098     }
00099     Q3ValueList<p2k_fileInfo> p2kfiles=p2kWrapper->getFiles();
00100     Q3ValueListIterator<p2k_fileInfo> fileInfo;
00101     QString fname;
00102     for(fileInfo=p2kfiles.begin(); fileInfo!=p2kfiles.end(); ++fileInfo)
00103     {
00104         fname=(*fileInfo).name;
00105         if(!fname.contains(regexp) ) continue;
00106 //         kDebug() <<"P2KImpl Listing files:" << fname.replace(regexp,"").mid(1);
00107         if(fname.replace(regexp, "").mid(1).contains(QDir::separator() ) ) continue;
00108         kDebug() <<"Listing files: `" << (*fileInfo).name <<"`";
00109         KIO::UDSEntry entry;
00110         createEntry(entry, url.url() + QDir::separator() + KUrl((*fileInfo).name).fileName(), *fileInfo);
00111         list.append(entry);
00112     }
00113 
00114     /*
00115 //     kDebug() <<"P2KImpl::" << dirList;
00116 //     QStringList::ConstIterator dirpath = dirList.begin();
00117 //     QStringList::ConstIterator end = dirList.end();
00118     QString s_entry;
00119     for(QStringList::Iterator dirpath=dirList.begin(); dirpath!=dirList.end(); ++dirpath)
00120     {
00121         s_entry=(*dirpath);
00122         if( ! s_entry.contains( url.path() ) ) continue;
00123         s_entry=s_entry.replace( 0,url.path().length(), "").mid(1);
00124         if(! s_entry.length() || s_entry.contains('/') ) continue;
00125         kDebug() <<"************ Creating entry" << s_entry;
00126         KIO::UDSEntry entry;
00127         entry.clear();
00128 //         QString dir_found=directory.append("/").append(*dirpath);
00129 //         if(!dir_found.find('/') ) dir_found=dir_found.mid(1);
00130         createEntry( entry, url.host().prepend("/") + "/" + *dirpath, s_entry);
00131         list.append(entry);
00132         names_found.append(*dirpath);
00133     }
00134     QValueList<p2k_fileInfo> p2kfiles=p2kWrapper->getFiles();
00135     QValueListIterator<p2k_fileInfo> fileInfo;
00136 
00137     for(fileInfo=p2kfiles.begin(); fileInfo!=p2kfiles.end(); ++fileInfo)
00138     {
00139         s_entry=(*fileInfo).name;
00140         if( ! s_entry.contains( url.path() ) ) continue;
00141         s_entry=s_entry.replace( 0,url.path().length(), "").mid(1);
00142         if(! s_entry.length() || s_entry.contains('/') ) continue;
00143         KIO::UDSEntry entry;
00144         entry.clear();
00145 //         QString dir_found=directory.append("/").append(*dirpath);
00146 //         if(!dir_found.find('/') ) dir_found=dir_found.mid(1);
00147         createEntry( entry, url.host().prepend("/") + "/" + url.path() , *fileInfo);
00148         list.append(entry);
00149         names_found.append((*fileInfo).name);
00150     }
00151     */
00152     return true;
00153 }
00154 
00155 
00156 static void addAtom(KIO::UDSEntry &entry, unsigned int ID, long l,
00157                     const QString &s = QString() )
00158 {
00159     KIO::UDSAtom atom;
00160     atom.m_uds = ID;
00161     atom.m_long = l;
00162     atom.m_str = s;
00163     entry.append(atom);
00164 }
00165 
00166 
00167 void P2KImpl::createTopLevelEntry(KIO::UDSEntry &entry) const
00168 {
00169     entry.clear();
00170     addAtom(entry, KIO::UDSEntry::UDS_NAME, 0, ".");
00171     addAtom(entry, KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
00172     addAtom(entry, KIO::UDSEntry::UDS_ACCESS, 0777);
00173     addAtom(entry, KIO::UDSEntry::UDS_MIME_TYPE, 0, "inode/directory");
00174     addAtom(entry, KIO::UDSEntry::UDS_ICON_NAME, 0, "p2k");
00175     addAtom(entry, KIO::UDSEntry::UDS_USER, 0, "root");
00176     addAtom(entry, KIO::UDSEntry::UDS_GROUP, 0, "root");
00177     addAtom(entry, KIO::UDSEntry::UDS_NAME, 0, ".");
00178 }
00179 
00180 void P2KImpl::createEntry(KIO::UDSEntry &entry, const KUrl &url, p2k_fileInfo &file)
00181 {
00182     kDebug() <<"Using ***NEW*** createEntry `" << url.url() <<"`";
00183     QString filepath=url.path();
00184     QString filename=url.fileName();
00185     QString directory = /*url.host() +*/ url.directory();
00186     kDebug() <<"File name: `" << filename <<"`; file path: `" << filepath <<"`; directory: `" << directory <<"`";
00187     
00188     KMimeType mimetype=(* KMimeType::findByURL( filepath, file.attr, false,  true));
00189     KDesktopFile desktop(filepath, true);
00190     kDebug() <<"KDesktopFile::" << desktop.fileName();
00191 
00192     entry.clear();
00193 
00194     addAtom(entry, KIO::UDSEntry::UDS_NAME, 0, filename);
00195     addAtom(entry, KIO::UDSEntry::UDS_SIZE, file.size);
00196     addAtom(entry, KIO::UDSEntry::UDS_URL, 0, url.url() );
00197     //  kDebug() <<"*******debug UDSEntry::UDS_URL: obex:/" << directory << filename;
00198     //  addAtom(entry, KIO::UDSEntry::UDS_FILE_TYPE, (S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH) );
00199     addAtom(entry, KIO::UDSEntry::UDS_FILE_TYPE, /*file.attr*/ S_IFREG );
00200 //     addAtom(entry, KIO::UDSEntry::UDS_CREATION_TIME, file.mtime );
00201     addAtom(entry, KIO::UDSEntry::UDS_MIME_TYPE, 0, mimetype.name() );
00202 
00203     kDebug() << filepath <<" mime type:" << mimetype.name() <<"; file attributes:" << QString::number(file.attr);
00204 
00205     QString icon = desktop.readIcon();
00206     QString empty_icon = desktop.readEntry("EmptyIcon");
00207 
00208  /*   if (!empty_icon.isEmpty())
00209     {
00210         KUrl url = desktop.readURL();
00211 
00212         m_lastListingEmpty = true;
00213 
00214         KIO::ListJob *job = KIO::listDir(url, KIO::HideProgressInfo, false);
00215         connect( job, SIGNAL( entries(KIO::Job *,
00216                  const KIO::UDSEntryList &) ),
00217                  this, SLOT( slotEntries(KIO::Job *,
00218                              const KIO::UDSEntryList &) ) );
00219         connect( job, SIGNAL( result(KIO::Job *) ),
00220                  this, SLOT( slotResult(KIO::Job *) ) );
00221         qApp->eventLoop()->enterLoop();
00222 
00223         if (m_lastListingEmpty) icon = empty_icon;
00224     }
00225                                        */
00226     addAtom(entry, KIO::UDSEntry::UDS_ICON_NAME, 0, icon);
00227 }
00228 
00229 
00230 void P2KImpl::createEntry(KIO::UDSEntry &entry,
00231                           const QString &directory,
00232                           const QString &file, bool isDrive)
00233 {
00234     kDebug() <<"P2KImpl::createEntry:" << directory <<"<->" << file;
00235 
00236     KDesktopFile desktop(directory+file, true);
00237 
00238     kDebug() <<"path =" << directory << file;
00239 
00240     entry.clear();
00241 
00242     addAtom(entry, KIO::UDSEntry::UDS_NAME, 0, file);
00243     addAtom(entry, KIO::UDSEntry::UDS_URL, 0,directory + QDir::separator() + file);
00244 
00245     addAtom(entry, KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
00246     if(isDrive)
00247         addAtom(entry, KIO::UDSEntry::UDS_MIME_TYPE, 0, "media/hdd_mounted");
00248     else addAtom(entry, KIO::UDSEntry::UDS_MIME_TYPE, 0, "inode/directory");
00249 
00250     QString icon = desktop.readIcon();
00251     QString empty_icon = desktop.readEntry("EmptyIcon");
00252 
00253     if (!empty_icon.isEmpty())
00254     {
00255         KUrl url = desktop.readURL();
00256 
00257         m_lastListingEmpty = true;
00258 
00259         KIO::ListJob *job = KIO::listDir(url, KIO::HideProgressInfo, false);
00260         connect( job, SIGNAL( entries(KIO::Job *,
00261                  const KIO::UDSEntryList &) ),
00262                  this, SLOT( slotEntries(KIO::Job *,
00263                              const KIO::UDSEntryList &) ) );
00264         connect( job, SIGNAL( result(KIO::Job *) ),
00265                  this, SLOT( slotResult(KIO::Job *) ) );
00266         qApp->eventLoop()->enterLoop();
00267 
00268         if (m_lastListingEmpty) icon = empty_icon;
00269     }
00270 
00271     addAtom(entry, KIO::UDSEntry::UDS_ICON_NAME, 0, icon);
00272 }
00273 
00274 void P2KImpl::createEntry(KIO::UDSEntry &entry,
00275                           const QString &directory,
00276                           p2k_fileInfo &file)
00277 {
00278     QString filename=file.name;
00279     filename=filename.mid(filename.findRev('/') +1);
00280     KMimeType mimetype=(* KMimeType::findByPath(filename, 0, true));
00281     KDesktopFile desktop(directory+file.name, true);
00282 
00283     entry.clear();
00284 
00285     addAtom(entry, KIO::UDSEntry::UDS_NAME, 0, filename);
00286     addAtom(entry, KIO::UDSEntry::UDS_SIZE, file.size);
00287 //     addAtom(entry, KIO::UDSEntry::UDS_URL, 0, "p2k:/"+directory + filename);
00288 //     kDebug() <<"*******debug UDSEntry::UDS_URL: p2k:/" << directory << filename;
00289     addAtom(entry, KIO::UDSEntry::UDS_FILE_TYPE, (S_IRUSR | S_IRGRP | S_IROTH | S_IXUSR | S_IXGRP | S_IXOTH) );
00290     // Attributes: 0=nothing, 2=hidden, 4=system, 1=readonly
00291 //     if (file.attr & 1 ) addAtom(entry, KIO::UDSEntry::UDS_ACCESS, 0555);
00292 //     if (file.attr & 2 ) addAtom( entry, KIO::UDSEntry::UDS_HIDDEN, true);
00293 //     if (file.attr & 4 );
00294     addAtom(entry, KIO::UDSEntry::UDS_MIME_TYPE, 0, mimetype.name() );
00295     kDebug() << filename <<" mime type:" << mimetype.name() <<"; file attributes:" << file.attr;
00296     QString icon = desktop.readIcon();
00297     QString empty_icon = desktop.readEntry("EmptyIcon");
00298 
00299     if (!empty_icon.isEmpty())
00300     {
00301         KUrl url = desktop.readURL();
00302 
00303         m_lastListingEmpty = true;
00304 
00305         KIO::ListJob *job = KIO::listDir(url, KIO::HideProgressInfo, false);
00306         connect( job, SIGNAL( entries(KIO::Job *,
00307                  const KIO::UDSEntryList &) ),
00308                  this, SLOT( slotEntries(KIO::Job *,
00309                              const KIO::UDSEntryList &) ) );
00310         connect( job, SIGNAL( result(KIO::Job *) ),
00311                  this, SLOT( slotResult(KIO::Job *) ) );
00312         qApp->eventLoop()->enterLoop();
00313 
00314         if (m_lastListingEmpty) icon = empty_icon;
00315     }
00316 
00317     addAtom(entry, KIO::UDSEntry::UDS_ICON_NAME, 0, icon);
00318 }
00319 
00320 void P2KImpl::slotEntries(KIO::Job *job, const KIO::UDSEntryList &list)
00321 {
00322     if (list.size()>0)
00323     {
00324         job->kill(true);
00325         m_lastListingEmpty = false;
00326         qApp->eventLoop()->exitLoop();
00327     }
00328 }
00329 
00330 void P2KImpl::slotResult(KIO::Job *)
00331 {
00332     qApp->eventLoop()->exitLoop();
00333 }
00334 
00335 
00336 #include "p2kimpl.moc"
00337 
00338 
00342 void P2KImpl::fetchFilesList()
00343 {
00344     p2kWrapper->fetchFileList();
00345 }
00346 
00347 
00351 int P2KImpl::getFile(const KUrl &url, char* buffer)
00352 {
00353     kDebug() <<"kio_p2k::getFile" << url.path();
00354     return p2kWrapper->getFile( url.path(), buffer );
00355 }
00356 
00357 
00361 bool P2KImpl::statEntry(const KUrl &url, KIO::UDSEntry &entry)
00362 {
00363     kDebug() <<"Stat for" << url.path();
00364 
00365     Q3ValueList<p2k_fileInfo> fileList=p2kWrapper->getFiles();
00366     Q3ValueListIterator<p2k_fileInfo> files_it;
00367     bool found=false;
00368     for(files_it=fileList.begin(); files_it!=fileList.end(); ++files_it)
00369     {
00370         if( url.path()==(*files_it).name )
00371         {
00372             found=true;
00373             break;
00374         }
00375     }
00376     if ( ! found ) return false;
00377     QString s_entry=(*files_it).name;
00378     kDebug() <<"s_entry(1) ==" << s_entry;
00379     if( ! s_entry.contains( url.path() ) ) return false;
00380     /*s_entry=s_entry.replace( 0,url.path().length(), "").mid(1);
00381     kDebug() <<"s_entry(2) ==" << s_entry;
00382 
00383     if(! s_entry.length() || s_entry.contains('/') ) return false;*/
00384     s_entry=QStringList::split('/', s_entry).last();
00385     kDebug() <<"OK, creating entry" << s_entry;
00386     entry.clear();
00387 //         QString dir_found=directory.append("/").append(*dirpath);
00388 //         if(!dir_found.find('/') ) dir_found=dir_found.mid(1);
00389     createEntry( entry, /*url.host().prepend("/") + "/" + url.path()*/ url , (*files_it) );
00390     return true;
00391 }

kmobiletools

Skip menu "kmobiletools"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal