~ubuntu-docviewer-dev/ubuntu-docviewer-app/lo-viewer

« back to all changes in this revision

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

Update framework to 'ubuntu-sdk-14.10'. Fixes: https://bugs.launchpad.net/bugs/1401718.

Approved by Nekhelesh Ramananthan, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013-2015 Canonical, Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3, as published
6
 
 * by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 */
17
 
 
18
 
#include "docviewerfile.h"
19
 
 
 
1
#include <QObject>
 
2
#include <QDebug>
 
3
#include <QFileInfo>
20
4
#include <QDir>
21
 
#include <QFileInfo>
22
5
#include <QMimeDatabase>
23
 
#include <QDateTime>
24
6
 
25
 
#include <QDebug>
 
7
#include "docviewerFile.h"
26
8
 
27
9
/*
28
10
 ----8<-----
29
11
 
30
 
 import DocumentViewer 1.0
 
12
 import org.docviewer.file 1.0
31
13
 
32
14
 Rectangle {
33
15
   width: 200
38
20
      path: "the/path/of/file"
39
21
 
40
22
      onMimetypeChanged: {
41
 
        do.something(mimetype.name);
 
23
        do.something(mimetype);
42
24
      }
43
25
   }
44
26
 
45
27
   Text {
46
28
     anchors.centerIn: parent
47
 
     text: mimetype.description
 
29
     text: helloType.helloworld
48
30
   }
49
31
 }
50
32
 
53
35
 
54
36
DocviewerFile::DocviewerFile(QObject *parent) :
55
37
    QObject(parent),
56
 
    m_path("")
57
 
{
58
 
    connect(this, SIGNAL(pathChanged()), this, SLOT(open()));
59
 
}
60
 
 
61
 
DocviewerFile::~DocviewerFile()
62
 
{
63
 
    //
64
 
}
65
 
 
66
 
void DocviewerFile::setPath(const QString &path)
67
 
{
68
 
    if (m_path == path)
69
 
        return;
70
 
 
71
 
    QFileInfo file(QDir::currentPath(), path);
72
 
 
73
 
    m_path = file.absoluteFilePath();
74
 
    Q_EMIT pathChanged();
75
 
 
76
 
    qDebug() << "[FILE] Path parsed as:" << m_path;
77
 
}
78
 
 
79
 
void DocviewerFile::open()
80
 
{
81
 
    if (m_path.isEmpty())
82
 
        return;
83
 
 
84
 
    QFileInfo file(m_path);
85
 
 
86
 
    if (file.exists()) {
87
 
        qDebug() << "[FILE] Extracting information from the file...";
88
 
 
89
 
        /** Get info of the file **/
90
 
        m_info["size"] = file.size();
91
 
        m_info["lastModified"] = file.lastModified();
92
 
        m_info["creationTime"] = file.created();
93
 
        Q_EMIT infoChanged();
94
 
 
95
 
        /** Get mimetype **/
96
 
        this->setMimetype();
 
38
    path("")
 
39
{
 
40
}
 
41
DocviewerFile::~DocviewerFile() {
 
42
    
 
43
}
 
44
 
 
45
void DocviewerFile::setPath(QString p) {
 
46
    if (p.isEmpty()) {
 
47
        this->path = "";
97
48
    }
98
 
 
99
49
    else {
100
 
        qDebug() << "[FILE] ERROR: Requested file does not exist!";
101
 
        m_error = -1;
102
 
        Q_EMIT errorChanged();
103
 
    }
104
 
}
105
 
 
106
 
void DocviewerFile::setMimetype()
 
50
        this->path = QFileInfo(QDir::currentPath(), p).absoluteFilePath();
 
51
    }
 
52
 
 
53
    qDebug() << "[FILE] Path parsed as:" << this->path;
 
54
 
 
55
    this->open();
 
56
 
 
57
    emit pathChanged();
 
58
}
 
59
 
 
60
void DocviewerFile::open() {
 
61
    if (!path.isEmpty())
 
62
    {
 
63
        QFileInfo file(path);
 
64
 
 
65
        if (file.exists()) {
 
66
            qDebug() << "[FILE] Extracting informations from the file...";
 
67
 
 
68
            /**Get info of the file**/
 
69
            size = file.size();
 
70
            emit sizeChanged();
 
71
 
 
72
            lastmodified = file.lastModified();
 
73
            emit lastmodifiedChanged();
 
74
 
 
75
            creationTime = file.created();
 
76
            emit creationTimeChanged();
 
77
 
 
78
            /**Get mimetype**/
 
79
            this->readMimeType();
 
80
        }
 
81
        else {
 
82
            qDebug() << "[FILE] ERROR: Requested file does not exist!";
 
83
            error = -1;
 
84
            emit errorChanged();
 
85
        }
 
86
    }
 
87
}
 
88
 
 
89
void DocviewerFile::readMimeType()
107
90
{
108
 
    QMimeType mime = QMimeDatabase().mimeTypeForFile(m_path);
109
 
 
110
 
    m_mimetype["name"] = mime.name();
111
 
    m_mimetype["description"] = mime.comment();
112
 
 
113
 
    // Use a more user-friendly value for 'name' when Qt can not determinate the
114
 
    // mime type.
115
 
    if (m_mimetype.value("name") == "application/octet-stream")
116
 
        m_mimetype["name"] = "Unknown";
117
 
 
118
 
    qDebug() << "[FILE] Requested file mime type:" << m_mimetype.value("name");
119
 
    Q_EMIT mimetypeChanged();
 
91
    QMimeDatabase db;
 
92
    mimetype = db.mimeTypeForFile(this->path).name();
 
93
 
 
94
    if (mimetype == "application/octet-stream") {
 
95
        // Returned by Qt when it cannot determinate the mime type
 
96
        mimetype = "Unknown";
 
97
    }
 
98
 
 
99
    qDebug() << "[FILE] Mime type for the requested file is" << mimetype;
 
100
    emit mimetypeChanged();
120
101
}