~verzegnassi-stefano/+junk/docviewer-tmp-001

« back to all changes in this revision

Viewing changes to src/plugin/file-qml-plugin/docviewerFile.cpp

  • Committer: Tarmac
  • Author(s): David Planella, Daniel Holbach, Fabio Colella, Stefano Verzegnassi, Arto Jalkanen
  • Date: 2014-10-29 07:39:08 UTC
  • mfrom: (31.1.22 add-plugin)
  • Revision ID: tarmac-20141029073908-8s6r18syexjo37ga
Adds the file plugin to the source tree.

Approved by Stefano Verzegnassi, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QProcess>
 
2
#include <QObject>
 
3
#include <QDebug>
 
4
#include <QFileInfo>
 
5
#include <QDir>
 
6
 
 
7
#include "docviewerFile.h"
 
8
 
 
9
/*
 
10
 ----8<-----
 
11
 
 
12
 import org.docviewer.file 1.0
 
13
 
 
14
 Rectangle {
 
15
   width: 200
 
16
   height: 200
 
17
 
 
18
   File {
 
19
      id: file
 
20
      path: "the/path/of/file"
 
21
 
 
22
      onMimetypeChanged: {
 
23
        do.something(mimetype);
 
24
      }
 
25
   }
 
26
 
 
27
   Text {
 
28
     anchors.centerIn: parent
 
29
     text: helloType.helloworld
 
30
   }
 
31
 }
 
32
 
 
33
 -----8<------
 
34
*/
 
35
 
 
36
DocviewerFile::DocviewerFile(QObject *parent) :
 
37
    QObject(parent),
 
38
    path("")
 
39
{
 
40
}
 
41
DocviewerFile::~DocviewerFile() {
 
42
    
 
43
}
 
44
 
 
45
void DocviewerFile::setPath(QString p) {
 
46
    if (p.isEmpty()) {
 
47
        this->path = "";
 
48
    }
 
49
    else {
 
50
        this->path = QFileInfo(QDir::currentPath(), p).absoluteFilePath();
 
51
    }
 
52
 
 
53
    this->open();
 
54
 
 
55
    emit pathChanged();
 
56
}
 
57
 
 
58
void DocviewerFile::open() {
 
59
    if (!path.isEmpty())
 
60
    {
 
61
        QFileInfo file(path);
 
62
 
 
63
        if (file.exists()) {
 
64
 
 
65
            /**Get info of the file**/
 
66
            size = file.size();
 
67
            emit sizeChanged();
 
68
 
 
69
            lastmodified = file.lastModified();
 
70
            emit lastmodifiedChanged();
 
71
 
 
72
            /**Get mimetype**/
 
73
            QStringList mimeTypeCommand;
 
74
            mimeTypeCommand << "-ib" << path;
 
75
 
 
76
            mimeTypeProcess = new QProcess();
 
77
 
 
78
            this->connect(mimeTypeProcess, SIGNAL(readyReadStandardOutput()), SLOT(s_readMimeType()));
 
79
            this->connect(mimeTypeProcess, SIGNAL(finished(int)), SLOT(s_finished(int)));
 
80
 
 
81
            mimeTypeProcess->start("file", mimeTypeCommand);
 
82
 
 
83
        }
 
84
        else {
 
85
            error = -1;
 
86
            emit errorChanged();
 
87
        }
 
88
    }
 
89
 
 
90
 
 
91
}
 
92
 
 
93
/****SLOT****/
 
94
 
 
95
void DocviewerFile::s_readMimeType()
 
96
{
 
97
    mimetype = mimeTypeProcess->readAllStandardOutput();
 
98
    mimetype = mimetype.left(mimetype.size()-1);
 
99
}
 
100
 
 
101
void DocviewerFile::s_finished(int exitCode)
 
102
{
 
103
    if (exitCode != 0)
 
104
        mimetype = "Unknown";
 
105
    emit mimetypeChanged();
 
106
}