-rw-r--r-- | core/obex/receiver.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/core/obex/receiver.cpp b/core/obex/receiver.cpp new file mode 100644 index 0000000..50ee6cb --- a/dev/null +++ b/core/obex/receiver.cpp | |||
@@ -0,0 +1,166 @@ | |||
1 | #include <sys/types.h> | ||
2 | #include <sys/stat.h> | ||
3 | #include <sys/mman.h> | ||
4 | #include <unistd.h> | ||
5 | |||
6 | #include <fcntl.h> | ||
7 | |||
8 | #include <qfile.h> | ||
9 | #include <qfileinfo.h> | ||
10 | #include <qlabel.h> | ||
11 | #include <qhbox.h> | ||
12 | #include <qtextview.h> | ||
13 | #include <qpushbutton.h> | ||
14 | |||
15 | #include <qpe/applnk.h> | ||
16 | #include <qpe/qpeapplication.h> | ||
17 | #include <qpe/qcopenvelope_qws.h> | ||
18 | |||
19 | #include "obex.h" | ||
20 | #include "receiver.h" | ||
21 | |||
22 | using namespace OpieObex; | ||
23 | |||
24 | Receiver::Receiver() { | ||
25 | m_obex = new Obex(this, "Receiver"); | ||
26 | connect(m_obex, SIGNAL(receivedFile(const QString& ) ), | ||
27 | this, SLOT(slotReceived(const QString& ) ) ); | ||
28 | m_obex->receive(); | ||
29 | } | ||
30 | Receiver::~Receiver() { | ||
31 | m_obex->setReceiveEnabled( false ); | ||
32 | delete m_obex; | ||
33 | } | ||
34 | void Receiver::slotReceived( const QString& file ) { | ||
35 | int check = checkFile(file); | ||
36 | if ( check == AddressBook ) | ||
37 | handleAddr( file ); | ||
38 | else if ( check == Datebook ) | ||
39 | handleDateTodo( file ); | ||
40 | else | ||
41 | handleOther( file ); | ||
42 | } | ||
43 | void Receiver::handleAddr( const QString& str ) { | ||
44 | QCopEnvelope e("QPE/Application/addressbook", "setDocument(QString)" ); | ||
45 | e << str; | ||
46 | } | ||
47 | /* we can not say for sure if it's a VEevent ot VTodo */ | ||
48 | void Receiver::handleDateTodo( const QString& str ) { | ||
49 | QCopEnvelope e0("QPE/Application/todolist", "setDocument(QString)"); | ||
50 | e0 << str; | ||
51 | QCopEnvelope e1("QPE/Application/datebook", "setDocument(QString)" ); | ||
52 | e1 << str; | ||
53 | } | ||
54 | /* | ||
55 | * Handle other asks if it should accept the | ||
56 | * beamed object and creates a DocLnk | ||
57 | */ | ||
58 | void Receiver::handleOther( const QString& other ) { | ||
59 | OtherHandler* hand = new OtherHandler(); | ||
60 | hand->handle( other ); | ||
61 | } | ||
62 | int Receiver::checkFile( const QString& file ) { | ||
63 | int ret; | ||
64 | if (file.right(4) == ".vcs" ) { | ||
65 | ret = Datebook; | ||
66 | }else if ( file.right(4) == ".vcf") { | ||
67 | ret = AddressBook; | ||
68 | }else | ||
69 | ret = Other; | ||
70 | |||
71 | return ret; | ||
72 | } | ||
73 | |||
74 | OtherHandler::OtherHandler() | ||
75 | : QVBox() | ||
76 | { | ||
77 | QHBox* box = new QHBox(this); | ||
78 | QLabel* lbl = new QLabel(box); | ||
79 | lbl->setText(tr("<qt><b>Received:</b></qt>")); | ||
80 | m_na = new QLabel(box); | ||
81 | |||
82 | QFrame* frame = new QFrame(this); | ||
83 | frame->setFrameShape( QFrame::HLine ); | ||
84 | frame->setFrameShadow( QFrame::Sunken ); | ||
85 | |||
86 | m_view = new QTextView(this); | ||
87 | |||
88 | box = new QHBox(this); | ||
89 | QPushButton *but = new QPushButton(box); | ||
90 | but->setText(tr("Accept") ); | ||
91 | connect(but, SIGNAL(clicked() ), | ||
92 | this, SLOT(accept()) ); | ||
93 | |||
94 | but = new QPushButton(box); | ||
95 | but->setText(tr("Deny") ); | ||
96 | connect(but, SIGNAL(clicked() ), | ||
97 | this, SLOT(deny() ) ); | ||
98 | |||
99 | raise(); | ||
100 | showMaximized(); | ||
101 | } | ||
102 | OtherHandler::~OtherHandler() { | ||
103 | |||
104 | } | ||
105 | void OtherHandler::handle( const QString& file ) { | ||
106 | m_file = file; | ||
107 | m_na->setText(file); | ||
108 | DocLnk lnk(file); | ||
109 | |||
110 | 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() ); | ||
111 | m_view->setText( str ); | ||
112 | } | ||
113 | |||
114 | /* | ||
115 | * hehe evil evil mmap ahead :) | ||
116 | * we quickly copy the file and then we'll create a DocLnk for it | ||
117 | */ | ||
118 | void OtherHandler::accept() { | ||
119 | QString na = targetName( m_file ); | ||
120 | copy(m_file, na ); | ||
121 | DocLnk lnk(na); | ||
122 | lnk.writeLink(); | ||
123 | QFile::remove(m_file); | ||
124 | delete this; | ||
125 | } | ||
126 | void OtherHandler::deny() { | ||
127 | QFile::remove( m_file ); | ||
128 | delete this; | ||
129 | } | ||
130 | QString OtherHandler::targetName( const QString& file ) { | ||
131 | QFileInfo info( file ); | ||
132 | QString newFile = QPEApplication::documentDir()+ "/"+ info.baseName(); | ||
133 | QString newFileBase = newFile; | ||
134 | |||
135 | int trie = 0; | ||
136 | while (QFile::exists(newFile + info.extension() ) ) { | ||
137 | newFile = newFileBase + "_"+QString::number(trie) ; | ||
138 | trie++; | ||
139 | } | ||
140 | newFile += info.extension(); | ||
141 | |||
142 | return newFile; | ||
143 | } | ||
144 | |||
145 | /* fast cpy */ | ||
146 | void OtherHandler::copy(const QString& src, const QString& file) { | ||
147 | int src_fd = ::open( QFile::encodeName( src ), O_RDONLY ); | ||
148 | int to_fd = ::open( QFile::encodeName( file), O_RDWR| O_CREAT| O_TRUNC, | ||
149 | S_IRUSR, S_IWUSR, S_IRGRP, S_IRGRP ); | ||
150 | |||
151 | struct stat stater; | ||
152 | ::fstat(src_fd, &stater ); | ||
153 | ::lseek(to_fd, stater.st_size-1, SEEK_SET ); | ||
154 | |||
155 | void *src_addr, *dest_addr; | ||
156 | src_addr = ::mmap(0, stater.st_size, PROT_READ, | ||
157 | MAP_FILE | MAP_SHARED, src_fd, 0 ); | ||
158 | dest_addr= ::mmap(0, stater.st_size, PROT_READ | PROT_WRITE, | ||
159 | MAP_FILE | MAP_PRIVATE, to_fd, 0 ); | ||
160 | |||
161 | ::memcpy(src_addr , dest_addr, stater.st_size ); | ||
162 | ::munmap(src_addr , stater.st_size ); | ||
163 | ::munmap(dest_addr, stater.st_size ); | ||
164 | |||
165 | // done | ||
166 | } | ||