ryxeo-webphotoalbum-git / tools.cpp @ 83a77071
Historique | Voir | Annoter | Télécharger (9,29 ko)
1 |
/** Classe tools: regroupe ce qui est mutualisable entre assistant et appli
|
---|---|
2 |
* @see https://redmine.ryxeo.com/projects/
|
3 |
* @author 2012 Eric Seigne <eric.seigne@ryxeo.com>
|
4 |
* @see The GNU Public License (GNU/GPL) v3
|
5 |
*
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or modify
|
8 |
* it under the terms of the GNU General Public License as published by
|
9 |
* the Free Software Foundation; either version 3 of the License, or
|
10 |
* (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful, but
|
13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
14 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
15 |
* for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License along
|
18 |
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
19 |
*/
|
20 |
|
21 |
|
22 |
#include "tools.h" |
23 |
|
24 |
class AbulEduSleeperThread : public QThread |
25 |
{ |
26 |
public:
|
27 |
static void msleep(unsigned long msecs) {QThread::msleep(msecs);} |
28 |
}; |
29 |
|
30 |
|
31 |
tools::tools(QDir basedir, QObject *parent) : |
32 |
QObject(parent) |
33 |
{ |
34 |
m_baseDir = basedir; |
35 |
|
36 |
m_web = new QWebView();
|
37 |
connect(m_web,SIGNAL(loadFinished(bool)),this,SLOT(unzipFinished(bool))); |
38 |
} |
39 |
|
40 |
void tools::ftpConnect(QString serveur, QString login, QString passwd)
|
41 |
{ |
42 |
qDebug() << "tools::ftpConnect (0)";
|
43 |
m_currentFTPFileUpload = -1;
|
44 |
|
45 |
m_ftp = new QFtp(this); |
46 |
connect(m_ftp, SIGNAL(commandFinished(int,bool)), |
47 |
this, SLOT(ftpCommandFinished(int,bool))); |
48 |
connect(m_ftp, SIGNAL(done(bool)),
|
49 |
this, SLOT(ftpDone(bool))); |
50 |
connect(m_ftp, SIGNAL(stateChanged(int)),
|
51 |
this, SLOT(ftpStateChanged(int))); |
52 |
connect(m_ftp, SIGNAL(listInfo(QUrlInfo)), |
53 |
this, SLOT(addToList(QUrlInfo)));
|
54 |
connect(m_ftp, SIGNAL(dataTransferProgress(qint64,qint64)), |
55 |
this, SLOT(updateDataTransferProgress(qint64,qint64)));
|
56 |
|
57 |
m_ftp->connectToHost(serveur, 21);
|
58 |
m_ftp->setTransferMode(QFtp::Passive); |
59 |
m_ftp->login(login, passwd); |
60 |
|
61 |
qDebug() << "tools::ftpConnect (1)";
|
62 |
} |
63 |
|
64 |
void tools::ftpCommandFinished(int commandId, bool error) |
65 |
{ |
66 |
qDebug() << "======================= BEGIN FTP INFO ======================= ";
|
67 |
qDebug() << " Numero de commande : " << commandId;
|
68 |
|
69 |
if (m_ftp->currentCommand() == QFtp::ConnectToHost) {
|
70 |
qDebug() << " Command: Connect to host";
|
71 |
} |
72 |
|
73 |
if (m_ftp->currentCommand() == QFtp::Login) {
|
74 |
qDebug() << " Command: Login";
|
75 |
} |
76 |
|
77 |
if (m_ftp->currentCommand() == QFtp::Put) {
|
78 |
QFile *file = static_cast<QFile*>(m_ftp->currentDevice());
|
79 |
if(file) {
|
80 |
file->deleteLater(); |
81 |
} |
82 |
qDebug() << " Command: Put";
|
83 |
} |
84 |
|
85 |
if (m_ftp->currentCommand() == QFtp::Get) {
|
86 |
qDebug() << " Command: Get";
|
87 |
} |
88 |
|
89 |
if (m_ftp->currentCommand() == QFtp::List) {
|
90 |
qDebug() << " Command: List";
|
91 |
} |
92 |
if(error) {
|
93 |
qDebug() << " Description de l'erreur " << m_ftp->errorString().trimmed();
|
94 |
qDebug() << " Abort command and clear pending commands !";
|
95 |
//grosse ruse pour remettre un peu a zero les erreurs de QFTP qui est quand meme
|
96 |
//bien capricieux !
|
97 |
m_ftp->cd("/");
|
98 |
m_ftp->list("/");
|
99 |
m_ftp->clearPendingCommands(); |
100 |
AbulEduSleeperThread::msleep(800);
|
101 |
} |
102 |
qDebug() << "======================= END FTP INFO ======================= ";
|
103 |
|
104 |
// if(m_currentFTPFileUpload >= 0 && m_fileListToUploadIndex.count() > 0) {
|
105 |
// if(m_typeUploadRecursif == "PHP")
|
106 |
// uploadRecursifPHP(m_destDirBase);
|
107 |
// else
|
108 |
// uploadRecursifGallery(m_destDirBase);
|
109 |
// }
|
110 |
// else {
|
111 |
// qDebug() << "Tout a ete expedie ";
|
112 |
// }
|
113 |
} |
114 |
|
115 |
void tools::ftpDone(bool error) |
116 |
{ |
117 |
// qDebug() << "tools::ftpDone : " << error;
|
118 |
// if(error)
|
119 |
// qDebug() << " " << m_ftp->errorString();
|
120 |
//on passe a la commande FTP suivante et on nettoie au passage
|
121 |
// m_ftp->clearPendingCommands();
|
122 |
ftpNextCommand(); |
123 |
} |
124 |
|
125 |
void tools::ftpStateChanged(int state) |
126 |
{ |
127 |
qDebug() << "tools::ftpStateChanged : code " << state;
|
128 |
if(state == QFtp::Closing) {
|
129 |
m_ftpCommands.clear(); |
130 |
} |
131 |
} |
132 |
|
133 |
void tools::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes)
|
134 |
{ |
135 |
// qDebug() << "Transfert: " << readBytes << " sur " << totalBytes;
|
136 |
emit signalUploadData(totalBytes, readBytes); |
137 |
} |
138 |
|
139 |
void tools::clear()
|
140 |
{ |
141 |
m_fileListToUpload.clear(); |
142 |
m_fileListToUploadIndex.clear(); |
143 |
m_currentFTPFileUpload = 0;
|
144 |
} |
145 |
|
146 |
int tools::nbTotalUpload()
|
147 |
{ |
148 |
return m_fileListToUpload.count()+2; |
149 |
} |
150 |
|
151 |
int tools::nbResteUpload()
|
152 |
{ |
153 |
return m_fileListToUploadIndex.count();
|
154 |
} |
155 |
|
156 |
void tools::ftpNextCommand()
|
157 |
{ |
158 |
if(!m_ftpCommands.isEmpty()) {
|
159 |
QStringList cmd(m_ftpCommands.takeFirst().split(";"));
|
160 |
QString fileName; |
161 |
//Commande speciale pour extraire un fichier ZIP sur le serveur
|
162 |
//exemple: http://webopenphoto.free.fr/sagc/extract.php?src=galleries/Cala-Montjoi-2012_Christophe/zip/mq.zip
|
163 |
if(cmd.at(0) == "unzip") { |
164 |
QUrl u(m_urlSite + "/extract.php?src=" + cmd.at(1)); |
165 |
m_web->load(u); |
166 |
qDebug() << "Ouverture de " << u;
|
167 |
fileName = "Unzip " + cmd.at(1); |
168 |
} |
169 |
if(cmd.at(0) == "mkdir") { |
170 |
m_ftp->mkdir(cmd.at(1));
|
171 |
fileName = cmd.at(1);
|
172 |
} |
173 |
if(cmd.at(0) == "put") { |
174 |
QFile *fic = new QFile(cmd.at(1)); |
175 |
if(fic->open(QIODevice::ReadOnly)) {
|
176 |
qDebug() << " Upload : " << cmd.at(1) << " -> " << cmd.at(2); |
177 |
m_ftp->put(fic,cmd.at(2));
|
178 |
fileName = cmd.at(2);
|
179 |
} |
180 |
else {
|
181 |
//erreur
|
182 |
} |
183 |
} |
184 |
m_currentFTPFileUpload++; |
185 |
emit signalUpload(nbTotalUpload(), m_currentFTPFileUpload, fileName); |
186 |
} |
187 |
else {
|
188 |
emit signalUploadEnd(); |
189 |
} |
190 |
} |
191 |
|
192 |
void tools::unzipFinished(bool status) |
193 |
{ |
194 |
ftpNextCommand(); |
195 |
} |
196 |
|
197 |
void tools::ftpUnzip(QString zipUrl)
|
198 |
{ |
199 |
qDebug() << "tools::ftpUnzip " << zipUrl;
|
200 |
m_ftpCommands << "unzip;" + zipUrl;
|
201 |
} |
202 |
|
203 |
void tools::ftpMkdir(QString rep)
|
204 |
{ |
205 |
//Avant de faire un mkdir il faut verifier si le repertoire n'existe pas deja
|
206 |
//sinon la pile des commandes FTP est remise a zero !!!
|
207 |
m_ftpCommands << "mkdir;" + rep;
|
208 |
} |
209 |
|
210 |
void tools::ftpPut(QString source, QString ladest)
|
211 |
{ |
212 |
m_ftpCommands << "put;" + source + ";" + ladest; |
213 |
} |
214 |
|
215 |
void tools::parcoursRecursif(QString rep, QString repertoireServeur)
|
216 |
{ |
217 |
qDebug() << "parcoursRecursif : " << rep;
|
218 |
|
219 |
QDir dir(rep); |
220 |
dir.setFilter(QDir::Hidden | QDir::Files | QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot); |
221 |
QFileInfoList list = dir.entryInfoList(); |
222 |
for(int i = 0; i < list.count(); i++) { |
223 |
qDebug() << " Ajout de " << list.at(i).absoluteFilePath() << " rep serveur : " << repertoireServeur; |
224 |
m_fileListToUpload.insert(list.at(i).absoluteFilePath(), repertoireServeur); |
225 |
m_fileListToUploadIndex << list.at(i).absoluteFilePath(); |
226 |
|
227 |
if(list.at(i).isDir()) {
|
228 |
if(list.at(i).fileName() == "hq" || list.at(i).fileName() == "mq" || list.at(i).fileName() == "lq" || list.at(i).fileName() == "thumbs") { |
229 |
qDebug() << " On ne parcours pas " << list.at(i).absoluteFilePath();
|
230 |
} |
231 |
else {
|
232 |
parcoursRecursif(list.at(i).absoluteFilePath(), list.at(i).absoluteFilePath().remove(m_baseDir.absolutePath()).remove("/data/www"));
|
233 |
} |
234 |
} |
235 |
} |
236 |
} |
237 |
|
238 |
|
239 |
void tools::uploadRecursifGallery(QString destDirBase, QString urlSite)
|
240 |
{ |
241 |
qDebug() << " ** uploadRecursifGallery : " << m_fileListToUploadIndex.count() ;
|
242 |
m_urlSite = urlSite; |
243 |
m_destDirBase = destDirBase; |
244 |
m_typeUploadRecursif = "Gallery";
|
245 |
while(m_fileListToUploadIndex.count()>0) { |
246 |
QString s(m_fileListToUploadIndex.takeFirst()); |
247 |
qDebug() << " -> " << s;
|
248 |
QFileInfo fi(s); |
249 |
if(fi.isDir()) {
|
250 |
QString ladest = m_destDirBase + "/galleries" + fi.absoluteFilePath().remove(m_baseDir.absolutePath());
|
251 |
qDebug() << " mkdir : " << ladest;
|
252 |
ftpMkdir(ladest); |
253 |
} |
254 |
else {
|
255 |
QString ladest = m_destDirBase + "/galleries/" + m_fileListToUpload.value(fi.absoluteFilePath()) + "/" + fi.fileName(); |
256 |
ftpPut(fi.absoluteFilePath(),ladest); |
257 |
if(fi.absoluteFilePath().endsWith(".zip")) { |
258 |
ftpUnzip("galleries/" + m_fileListToUpload.value(fi.absoluteFilePath()) + "/" + fi.fileName()); |
259 |
} |
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
|
265 |
void tools::uploadRecursifPHP(QString destDirBase)
|
266 |
{ |
267 |
qDebug() << "uploadRecursifPHP : " << m_fileListToUploadIndex.count() ;
|
268 |
m_destDirBase = destDirBase; |
269 |
m_typeUploadRecursif = "PHP";
|
270 |
|
271 |
while(m_fileListToUploadIndex.count()>0) { |
272 |
QString s(m_fileListToUploadIndex.takeFirst()); |
273 |
qDebug() << " -> " << s;
|
274 |
QFileInfo fi(s); |
275 |
if(fi.isDir()) {
|
276 |
QString ladest = m_destDirBase + fi.absoluteFilePath().remove(m_baseDir.absolutePath()).remove("/data/www");
|
277 |
qDebug() << "mkdir : " << ladest;
|
278 |
ftpMkdir(ladest); |
279 |
} |
280 |
else {
|
281 |
QString ladest = m_destDirBase + m_fileListToUpload.value(fi.absoluteFilePath()) + "/" + fi.fileName();
|
282 |
ftpPut(fi.absoluteFilePath(),ladest); |
283 |
} |
284 |
} |
285 |
} |
286 |
|
287 |
void tools::addToList(const QUrlInfo &urlInfo) |
288 |
{ |
289 |
qDebug() << "tools::addToList";
|
290 |
qDebug() << urlInfo.name(); |
291 |
} |