00001 #include "obex.h"
00002 #include "receiver.h"
00003 using namespace OpieObex;
00004
00005
00006 #include <opie2/odebug.h>
00007 #include <qpe/applnk.h>
00008 #include <qpe/qpeapplication.h>
00009 #include <qpe/qcopenvelope_qws.h>
00010 #include <qpe/filemanager.h>
00011 using namespace Opie::Core;
00012
00013
00014 #include <qfileinfo.h>
00015 #include <qlabel.h>
00016 #include <qtextview.h>
00017 #include <qpushbutton.h>
00018
00019
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <sys/mman.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <fcntl.h>
00026
00027
00028
00029 Receiver::Receiver() {
00030 m_obex = new Obex(this, "Receiver");
00031 connect(m_obex, SIGNAL(receivedFile(const QString&) ),
00032 this, SLOT(slotReceived(const QString&) ) );
00033 m_obex->receive();
00034 }
00035 Receiver::~Receiver() {
00036 m_obex->setReceiveEnabled( false );
00037 delete m_obex;
00038 }
00039 void Receiver::slotReceived( const QString& _file ) {
00040 QString file = _file;
00041 int check = checkFile(file);
00042 if ( check == AddressBook )
00043 handleAddr( file );
00044 else if ( check == Datebook )
00045 handleDateTodo( file );
00046 else
00047 handleOther( file );
00048 }
00049 void Receiver::handleAddr( const QString& str ) {
00050 QCopEnvelope e("QPE/Application/addressbook", "setDocument(QString)" );
00051 e << str;
00052 }
00053
00054 void Receiver::handleDateTodo( const QString& str ) {
00055 QCopEnvelope e0("QPE/Application/todolist", "setDocument(QString)");
00056 e0 << str;
00057 QCopEnvelope e1("QPE/Application/datebook", "setDocument(QString)" );
00058 e1 << str;
00059 }
00060
00061
00062
00063
00064 void Receiver::handleOther( const QString& other ) {
00065 OtherHandler* hand = new OtherHandler();
00066 hand->handle( other );
00067 }
00068 void Receiver::tidyUp( QString& _file, const QString& ending) {
00069
00070 QString file = _file;
00071 char foo[24];
00072 (void)::strcpy(foo, "/tmp/opie-XXXXXX");
00073
00074 int fd = ::mkstemp(foo);
00075
00076 if ( fd == -1 )
00077 return;
00078
00079 (void)::strncat( foo, QFile::encodeName(ending), 4 );
00080 _file = QString::fromLocal8Bit( foo );
00081 QString cmd = QString("sed -e \"s/^\\(X-MICROSOFT-BODYINK\\)\\;/\\1:/;\" < %2 > %2 ").arg( Global::shellQuote(file)).arg( Global::shellQuote(_file) );
00082 (void)::system( QFile::encodeName(cmd) );
00083
00084 cmd = QString("rm %1").arg( Global::shellQuote(file) );
00085 (void)::system( QFile::encodeName(cmd) );
00086 }
00087 int Receiver::checkFile( QString& file ) {
00088 int ret;
00089 QString ending;
00090
00091 if (file.right(4) == ".vcs" ) {
00092 ret = Datebook;
00093 ending = QString::fromLatin1(".vcs");
00094 }else if ( file.right(4) == ".vcf") {
00095 ret = AddressBook;
00096 ending = QString::fromLatin1(".vcf");
00097 }else
00098 ret = Other;
00099
00100
00101 if (ending.isEmpty() )
00102 return ret;
00103
00110 tidyUp( file, ending );
00111
00112 return ret;
00113 }
00114
00115
00116
00117 OtherHandler::OtherHandler()
00118 : QVBox()
00119 {
00120 QHBox* box = new QHBox(this);
00121 QLabel* lbl = new QLabel(box);
00122 lbl->setText(tr("<qt><b>Received:</b></qt>"));
00123 m_na = new QLabel(box);
00124
00125 QFrame* frame = new QFrame(this);
00126 frame->setFrameShape( QFrame::HLine );
00127 frame->setFrameShadow( QFrame::Sunken );
00128
00129 m_view = new QTextView(this);
00130
00131 box = new QHBox(this);
00132 QPushButton *but = new QPushButton(box);
00133 but->setText(tr("Accept") );
00134 connect(but, SIGNAL(clicked() ),
00135 this, SLOT(accept()) );
00136
00137 but = new QPushButton(box);
00138 but->setText(tr("Deny") );
00139 connect(but, SIGNAL(clicked() ),
00140 this, SLOT(deny() ) );
00141
00142 raise();
00143 showMaximized();
00144 }
00145 OtherHandler::~OtherHandler() {
00146
00147 }
00148 void OtherHandler::handle( const QString& file ) {
00149 m_file = file;
00150 m_na->setText(file);
00151 DocLnk lnk(file);
00152
00153 QString str = tr("<p>You received a file of type %1 (<img src=\"%2\"> )What do you want to do?").arg(lnk.type() ).arg(lnk.icon() );
00154 m_view->setText( str );
00155 }
00156
00157
00158
00159
00160
00161 void OtherHandler::accept() {
00162 QString na = targetName( m_file );
00163 copy(m_file, na );
00164 DocLnk lnk(na);
00165 lnk.writeLink();
00166 QFile::remove(m_file);
00167 delete this;
00168 }
00169 void OtherHandler::deny() {
00170 QFile::remove( m_file );
00171 delete this;
00172 }
00173 QString OtherHandler::targetName( const QString& file ) {
00174 QFileInfo info( file );
00175
00176
00177 Global::createDocDir();
00178
00179 QString newFile = QPEApplication::documentDir()+ "/"+ info.baseName();
00180 QString newFileBase = newFile;
00181
00182 int trie = 0;
00183 while (QFile::exists(newFile + "."+info.extension() ) ) {
00184 newFile = newFileBase + "_"+QString::number(trie) ;
00185 trie++;
00186 }
00187 newFile += "." + info.extension();
00188
00189 return newFile;
00190 }
00191
00192
00193 void OtherHandler::copy(const QString& src, const QString& file) {
00194 FileManager *fm;
00195 if(!fm->copyFile(src,file)) {
00196 owarn << "Copy failed" << oendl;
00197 }
00198 }