]> git.nothing2do.fr Git - diary-mobile.git/commitdiff
I download file from the web ! (I have to parse/write data)
authorNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Fri, 27 Dec 2013 16:16:58 +0000 (17:16 +0100)
committerNorbert Moutarde <norbert.moutarde@nothing2do.eu>
Fri, 27 Dec 2013 16:16:58 +0000 (17:16 +0100)
insert-sql-android.sh
mainwindow.cpp
mainwindow.h
ui.sql

index 82dd89420a6608c4137e9ec9cbb3ddb862b2918a..3c2e50a58f67476cc6b5f950d6d2d9fd75f06733 100755 (executable)
@@ -1,11 +1,13 @@
-export DB=/data/data/eu.nothing2do.diary-mobile/files/db
+export DB=/data/data/eu.nothing2do.diarymobile/files/db
+export TMP=/storage/sdcard0
 adb pull $DB
 echo "$DB file retreived"
 cat $1|sqlite3 db
-echo "db file populated"
-adb push db /storage/sdcard1
-echo "file db copied to /storage/sdcard1"
-adb shell su -c mv /storage/sdcard1/db $DB
-echo "file mv'ed to the right place"
+echo "$1 is into db file"
+adb push db $TMP
+echo "file db copied to $TMP"
+adb shell su -c cp $TMP/db $DB
+adb shell su -c rm $TMP/db
+echo "file cp'ed to the right place"
 adb shell su -c chown u0_a12:u0_a12 $DB
 echo "file chown'ed"
index 44c6e8fb89a014ff7dc19007525cdb44fc041e63..386f19f8f46b45d2cd340fdd0c21d05b4d39df30 100644 (file)
@@ -136,20 +136,23 @@ void MainWindow::action(const QString &a){
     flush();
     if (a.split(QString(" "))[0]==QString("set")){
         conf->setValue(QString(a.split(QString(" "))[1]), a.split(QString(" "))[2]);
-        start();
+        getButtons(way.last());
     }
     else if(a.split(QString(" "))[0]==QString("settext")){
         conf->setValue(a.split(QString(" "))[1], QInputDialog::getText(this, "setText", a.split(QString(" "))[1], QLineEdit::Normal, conf->value(QString(a.split(QString(" "))[1])).toString()));//conf->value(QString(a.split(QString(" ")).mid(2)))
+        getButtons(way.last());
     }
     else if(a.split(QString(" "))[0]==QString("setint")){
         conf->setValue(a.split(QString(" "))[1], QInputDialog::getInt(this, "setInt", a.split(QString(" "))[2] , conf->value(QString(a.split(QString(" "))[3])).toInt()));
+        getButtons(way.last());
     }
     else if(a.split(QString(" "))[0]=="new"){
         entry(a.remove(0, 4));
-        start();
+        getButtons(way.last());
     }
     else if(a.split(QString(" "))[0]=="setfile"){
         conf->setValue(a.split(QString(" "))[1], QFileDialog::getSaveFileName(this, QString("get file"), QDir::homePath()));
+        getButtons(way.last());
     }
     else getButtons(a);
 }
@@ -254,6 +257,8 @@ void MainWindow::firstrun(){
     conf->setValue(QString("menu"), QVariant(1));
     conf->setValue(QString("sqlfile"), QVariant("ui.sql"));
     conf->setValue(QString("getsql"), QVariant("wget --no-check-certificate https://github.com/Nothing2Do/diary-mobile-android/raw/master/ui.sql"));
+    conf->setValue(QString("update"), QVariant("https://raw.github.com/Nothing2Do/diary-mobile-android/master/ui.sql"));
+
 }
 QString &MainWindow::get(int row, int column){
     q->seek(row);
@@ -268,10 +273,10 @@ int MainWindow::exec(const QList<QString> & a){
     }
     qDebug()<<"exec("<<a[0]<<")="<<q->exec();
     QSqlError b=q->lastError();
-    int qs=q->size();
-    //q->exec(QString("COMMIT;"));
+
     if (b.isValid())qDebug()<<"SQL error->exec() :"<<b.text();
-    return qs;
+    if(q->isSelect())return q->size();
+    else return 0;
 }
 int MainWindow::alias(const QString & a){
     qDebug()<<"alias("<<a<<")";
@@ -292,8 +297,7 @@ int MainWindow::alias(const QString & a){
 
 }
 void MainWindow::entry(const QString &a){
-    QDateTime v;
-    QString time=v.currentDateTime().toString(QString("yyyy-MM-dd hh:mm:ss")), entry="";
+    QString time=QDateTime::currentDateTime().toString(QString("yyyy-MM-dd hh:mm:ss")), entry="";
     entry=QInputDialog::getText(this, QString("nouvelle entrée"), time, QLineEdit::Normal, a);
     if(entry=="")
         getButtons(way.last());
@@ -304,12 +308,10 @@ void MainWindow::entry(const QString &a){
     flux << c << endl;
     fichier.close();
     qDebug()<<"content added into file : "<<a;
-    q->prepare("INSERT INTO raw (date, texte) VALUES (:date, :txt);");
-    q->bindValue(":date", time);
-    q->bindValue(":txt", entry);
-    qDebug()<<":date = "<<q->boundValue(QString(":date"));
-    qDebug()<<":txt = "<<q->boundValue(QString(":txt"));
-    q->exec();
+    QList<QString> d({"INSERT INTO raw (date, texte) VALUES (:date, :txt);"});
+    d.append(time);
+    d.append(entry);
+    exec(d);
     QSqlError b=q->lastError();
     if (b.isValid())qDebug()<<"q->lastError():"<<b.text();
 }
@@ -340,8 +342,7 @@ void MainWindow::addButtonDB(){
 void MainWindow::getButtons(const QString &a){
     qDebug()<<"getButtons()";
     QList<QString> tmp({"select label,action from ui where keyword like :word;", a});;
-    QString b=QString::number(way.size());
-    label->setText(b);
+    label->setText(QString::number(way.size()));
     way.append(a);
     exec(tmp);
     int labCol = 0;//r.indexOf("label");
@@ -364,17 +365,17 @@ void MainWindow::editButtonDB(){
     }*/
 }
 void MainWindow::updateUi(){
-    // it work on linux
-    system(conf->value(QString("getsql")).toString().toStdString().c_str());
-    QString cmd="cat "+conf->value(QString("dbfile")).toString()+"|sqlite3 "+conf->value(QString("database")).toString();
-    qDebug()<<"cmd : "<<cmd;
-    system(cmd.toStdString().c_str());
-    return;
+    qDebug()<<"updateUi() : "<<conf->value(QString("update")).toString();
+    QUrl update(conf->value(QString("update")).toString());
+    m = new FileDownloader(update, this);
+    connect(m, SIGNAL(downloaded()), SLOT(writeUpdate()));
+
+
 
 }
 void MainWindow::back(){
     bool ok;
-    int a=QInputDialog::getInt(this, QString("back()"), QString("Ou ?"), way.size()-2, 0, way.size()-2, 1, &ok);
+    int a=QInputDialog::getInt(this, QString("back()"), QString("Quel page ?"), way.size()-2, 0, way.size()-2, 1, &ok);
     if(ok){
         flush();
         getButtons(way[a]);
@@ -404,3 +405,31 @@ static void SetTextToLabel(QLabel *label, QString text)
     QString clippedText = metrix.elidedText(text, Qt::ElideRight, width);
     label->setText(clippedText);
 }
+FileDownloader::FileDownloader(QUrl url, QObject *parent) : QObject(parent)
+{
+    connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
+                SLOT(fileDownloaded(QNetworkReply*)));
+
+    QNetworkRequest request(url);
+    m_WebCtrl.get(request);
+}
+FileDownloader::~FileDownloader()
+{
+
+}
+void FileDownloader::fileDownloaded(QNetworkReply* pReply)
+{
+    m_DownloadedData = pReply->readAll();
+    //emit a signal
+    pReply->deleteLater();
+    emit downloaded();
+}
+QByteArray FileDownloader::downloadedData() const
+{
+    return m_DownloadedData;
+}
+void MainWindow::writeUpdate(){
+    qDebug()<<"downloaded :"<<m->downloadedData();
+    /* put newly downloaded sql file into database */
+
+}
index 14de854359b5b59fd75db6789dba579d8ec69f51..91a0da2b56ca64bd7319c8494b16eef0039b76c6 100644 (file)
 //#include <QTableWidget>
 #include <QTextEdit>
 
+#ifndef FILEDOWNLOADER_H
+#define FILEDOWNLOADER_H
+
+#include <QObject>
+#include <QByteArray>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+
+class FileDownloader : public QObject
+{
+    Q_OBJECT
+public:
+    explicit FileDownloader(QUrl url, QObject *parent = 0);
+
+    virtual ~FileDownloader();
+
+    QByteArray downloadedData() const;
+
+signals:
+        void downloaded();
+
+private slots:
+
+    void fileDownloaded(QNetworkReply* pReply);
+
+private:
+
+    QNetworkAccessManager m_WebCtrl;
+
+    QByteArray m_DownloadedData;
+
+};
+
+#endif // FILEDOWNLOADER_H
+
+
 class CLabel : public QLabel
 {
 Q_OBJECT
@@ -38,6 +75,7 @@ protected:
     void mousePressEvent ( QMouseEvent * event ) ;
     void mouseReleaseEvent(QMouseEvent * event ) ;
 };
+
 class MainWindow : public QMainWindow
 {
     Q_OBJECT
@@ -82,6 +120,7 @@ private slots:
     void getButtons(const QString &);
     //void getButtons();
     void updateUi();
+    void writeUpdate();
 public slots:
 
 signals:
@@ -101,6 +140,7 @@ private:
     QSqlDatabase db;
     QSqlQuery *q;
     //QSqlRecord r;
+    FileDownloader * m;
 };
 
 
diff --git a/ui.sql b/ui.sql
index fc44ef955bbf0e6b5d2b922eac4ecd5a9cf09708..7092444d6967c93761089b5f451fd933ea81ff11 100644 (file)
--- a/ui.sql
+++ b/ui.sql
@@ -28,6 +28,6 @@ INSERT INTO "ui" (keyword, label, action) VALUES('config','database','settext db
 INSERT INTO "ui" (keyword, label, action) VALUES('config','filesave','settext file');
 INSERT INTO "ui" (keyword, label, action) VALUES('config','SQLsettings', 'settext SQLsettings requiressl=1 connect_timeout=10000');
 INSERT INTO "ui" (keyword, label, action) VALUES('config','button', 'settext button background-color: red;border-style: outset;border-width: 7px;border-radius: 10px;border-color: beige;font: bold 15px;padding: 6px;');
-INSERT INTO "ui" (keyword, label, action) VALUES('config','fichier SQL', 'setfile sqlfile ui.sql');
+INSERT INTO "ui" (keyword, label, action) VALUES('config','fichier SQL', 'settext sqlfile ui.sql');
 INSERT INTO "ui" (keyword, label, action) VALUES('config','getSQL', 'settext getsql wget --no-check-certificate https://github.com/Nothing2Do/diary-mobile-android/raw/master/ui.sql');
 COMMIT;