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

kmobiletools

obexwrapper.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 *   Copyright (C) 2006 by                                                 *
00003 *   Marcin Przylucki <marcin.przylucki@kdemail.net>                       *
00004 *   Marco Gulino <marco@kmobiletools.org>                                 *
00005 *                                                                         *
00006 *   This program is free software; you can redistribute it and/or modify  *
00007 *   it under the terms of the GNU General Public License as published by  *
00008 *   the Free Software Foundation; either version 2 of the License, or     *
00009 *   (at your option) any later version.                                   *
00010 *                                                                         *
00011 *   This program is distributed in the hope that it will be useful,       *
00012 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014 *   GNU General Public License for more details.                          *
00015 *                                                                         *
00016 *   You should have received a copy of the GNU General Public License     *
00017 *   along with this program; if not, write to the                         *
00018 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,       *
00019 *  Boston, MA 02110-1301, USA.                                            *
00020 ***************************************************************************/
00021 
00022 
00023 #include "obexwrapper.h"
00024 #include <kdebug.h>
00025 
00026 #include <iostream>
00027 #include <unistd.h>
00028 //Added by qt3to4:
00029 #include <Q3ValueList>
00030 
00031 using namespace std;
00032 
00033 OBEXWrapper *OBEXWrapper::p_instance=0;
00034 
00035 
00036 OBEXWrapper::OBEXWrapper( QObject *parent, const char *name) : QObject(parent, name),
00037     client(0), n_files(0)
00038 {
00039     p_instance=this;
00040 }
00041 
00042 void OBEXWrapper::setupParameters(  int transport,
00043                             int channel,
00044                             const QString &device,
00045                             const uint8_t *use_uuid,
00046                             int use_uuid_len,
00047                             int use_conn,
00048                             int use_path)
00049 {
00050     i_transport = transport;
00051     i_channel = channel;
00052     c_device = device;
00053     c_use_uuid = use_uuid;
00054     i_use_conn = use_conn;
00055     i_use_path = use_path;
00056     i_use_uuid_len = use_uuid_len;
00057     client=0;
00058     kDebug() <<"Initialized obex lib";
00059     n_files=0;
00060 }
00061 
00062 OBEXWrapper::~OBEXWrapper()
00063 {
00064     kDebug() <<"OBEXWrapper::~OBEXWrapper()";
00065     kDebug() <<"OBEXWrapper::ClosingPhone";
00066 
00067     if (client != NULL) {
00068         /* Disconnect */
00069         (void) obexftp_disconnect (client);
00070         /* Close */
00071         obexftp_close (client);
00072     }
00073 }
00074 
00075 
00076 static void info_cb(int event, const char *msg, int len, void *data)
00077 {
00078     static unsigned int i = 0;
00079     char progress[] = "\\|/-";
00080     kDebug() <<"Info_cb:";
00081 
00082     switch (event) {
00083 
00084     case OBEXFTP_EV_ERRMSG:
00085         kDebug() <<"Error:" << msg;
00086         i=0;
00087         break;
00088 
00089     case OBEXFTP_EV_ERR:
00090         kDebug() <<"failed:" << msg;
00091         i=0;
00092         break;
00093     case OBEXFTP_EV_OK:
00094         kDebug() <<"done";
00095         i=0;
00096         break;
00097 
00098     case OBEXFTP_EV_CONNECTING:
00099         kDebug() <<"Connecting...";
00100 //        fprintf(stderr, "");
00101         break;
00102     case OBEXFTP_EV_DISCONNECTING:
00103         kDebug() <<"Disconnecting...";
00104         break;
00105     case OBEXFTP_EV_SENDING:
00106         kDebug() <<"Sending \"" << msg <<"\"...";
00107         break;
00108     case OBEXFTP_EV_RECEIVING:
00109         kDebug() <<"Reciving \"" << msg <<"\"...";
00110         break;
00111 
00112     case OBEXFTP_EV_LISTENING:
00113         kDebug() <<"Waiting for incoming connection";
00114         break;
00115 
00116     case OBEXFTP_EV_CONNECTIND:
00117         kDebug() <<"Incoming connection";
00118         break;
00119     case OBEXFTP_EV_DISCONNECTIND:
00120         kDebug() <<"Disconnecting";
00121         break;
00122 
00123     case OBEXFTP_EV_INFO:
00124         kDebug() <<"Got info" << msg <<":"; // 64 bit problems ?
00125         break;
00126 
00127     case OBEXFTP_EV_BODY:
00128         //if (c == 'l' || c == 'X' || c == 'P') {
00129             if (msg == NULL)
00130                 kDebug() <<"No body.";
00131             else if (len == 0)
00132                 kDebug() <<"Empty body.";
00133             else
00134                 kDebug() << msg;
00135                 write(STDOUT_FILENO, msg, len);
00136                 ;
00137         //}  <- if
00138         break;
00139 
00140     case OBEXFTP_EV_PROGRESS:
00141         kDebug() <<"Progress:" << i++ <<";" << progress[i % strlen(progress) ];
00142         kDebug() <<"Current buffer size:" << OBEXWrapper::instance()->bufferSize();
00143         break;
00144     }
00145 }
00146 
00147 /*
00148 
00149 extern "C" { QValueList<obex_fileInfo> kioobex_fileList; }
00150 
00151 extern "C"
00152         void onFile( obex_fileInfo file)
00153 {
00154     kioobex_fileList+=(file);
00155 }
00156 */
00157 
00158 bool OBEXWrapper::connectClient()
00159 {
00160     kDebug() <<" OBEXWrapper::connectClient(): checking client;";
00161     if (client)
00162     {
00163         kDebug() <<"Already connected";
00164         return true;
00165     }
00166     kDebug() <<"Opening client, transport:" << i_transport;
00167         /* Open */
00168     client = obexftp_open (i_transport, NULL, info_cb, NULL);
00169     if(!client) {
00170         kDebug() <<"Error opening obexftp-client";
00171         return false;
00172     }
00173     if (!i_use_conn) {
00174         client->quirks &= ~OBEXFTP_CONN_HEADER;
00175     }
00176     if (!i_use_path) {
00177         client->quirks &= ~OBEXFTP_SPLIT_SETPATH;
00178     }
00179     for (int retry = 0; retry < 3; retry++) {
00180         kDebug() <<"Connecting:" << c_device;
00181         /* Connect*/
00182         if (obexftp_connect_uuid (client, c_device, i_channel , UUID_FBS, sizeof(UUID_FBS)) >= 0) {
00183             kDebug() <<"Phone connected in obex mode";
00184             return true;
00185         }
00186         sleep (2);
00187         kDebug() <<"Still trying to connect ( try" << retry <<" )";
00188         perror("Connection error: ");
00189     } // <- for
00190 
00191     obexftp_close(client);
00192     client = 0L;
00193     return false;
00194 }
00195 
00196 void OBEXWrapper::disconnectClient()
00197 {
00198     kDebug() <<"OBEXWrapper::disconnectClient()";
00199 
00200     if (client != NULL) {
00201         /* Disconnect */
00202         (void) obexftp_disconnect (client);
00203         /* Close */
00204         obexftp_close (client);
00205         client = NULL;
00206     }
00207 
00208     kDebug() <<"**************** Client disconnected";
00209 }
00210 
00211 bool OBEXWrapper::fetchFileList(const QString &path) 
00212 {
00213     kDebug() <<"OBEXWrapper::fetchFileList()";
00214 
00215     files.clear();
00216     n_files = 0;
00217 
00218     Q3ValueList<stat_entry_t>::Iterator it = files.begin();
00219 
00220     if( connectClient() ) {
00221         /* List folder */
00222         //stat_entry_t *ent;
00223         void *dir = obexftp_opendir(client, path.latin1() );
00224         while ( true ) {
00225             
00226             stat_entry_t* ent;
00227             if ( (ent = obexftp_readdir(dir)) == NULL ) break;
00228             files.append(*ent);
00229             n_files++;
00230         }
00231         obexftp_closedir(dir);
00232 
00233         kDebug() <<"OBEXWrapper::fetchFileList() number of files:" << n_files;
00234         
00235         return true;
00236     }
00237     return false;
00238 }
00239 
00245 int OBEXWrapper::getFile(const QString &path)
00246 {
00247     kDebug() <<"OBEXWrapper::getFile(" << path <<",...)";
00248     
00249 
00250     if( ! connectClient() ) return 0;
00251 
00252     if( obexftp_get(client, NULL, path.latin1() ) ) return 0;
00253 
00254     return 1;
00255 
00256 }
00257 
00258 int OBEXWrapper::putFile( const QString &path, char* buffer, int size)
00259 {
00260     kDebug() <<"OBEXWrapper::putFile(" << path <<",.......,......)";
00261     if( ! connectClient() ) return -1;
00262     kDebug() <<"Writing" << path <<" to the client, size:" << size <<" bytes.";
00263 
00264     int ret = obexftp_put_data( client, buffer, size, path.latin1() );
00265 
00266     return ret;
00267 }
00268 
00269 
00270 
00271 bool OBEXWrapper::deleteFile( const QString &path )
00272 {
00273     kDebug() <<"OBEXWrapper::deleteFile(" << path <<")";
00274     if( ! connectClient() ) return false;
00275 
00276     bool ret = obexftp_del( client, path.latin1() );
00277 
00278     return ret;
00279 }
00280 
00281 bool OBEXWrapper::mkDir( const QString &path )
00282 {
00283     kDebug() <<"OBEXWrapper::makeDir(" << path <<")";
00284     if( ! connectClient() ) return false;
00285 
00286     bool ret = obexftp_mkpath( client, path.latin1() );
00287 
00288     return ret;
00289 }
00290 

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
  •   doc
  • 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