~mzanetti/+junk/ubtd

« back to all changes in this revision

Viewing changes to app/obexd.cpp

  • Committer: Michael Zanetti
  • Date: 2016-04-14 23:34:10 UTC
  • Revision ID: michael.zanetti@canonical.com-20160414233410-17jg7ez3w1qc5e16
make it work, polish a bit, 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
Obexd::Obexd(QObject *parent) :
8
8
    QAbstractListModel(parent),
9
9
    m_dbus(QDBusConnection::sessionBus()),
10
 
    m_manager("org.openobex", "/", "org.openobex.Manager", m_dbus)
 
10
    m_manager("org.bluez.obex", "/org/bluez/obex", "org.bluez.obex.AgentManager1", m_dbus)
11
11
{
12
12
    qDebug() << "creating agent on dbus";
13
13
 
19
19
    if (!reply.isValid())
20
20
            qWarning() << "Error registering agent for the default adapter:" << reply.error();
21
21
 
22
 
 
23
 
    qDebug() << "done" << reply.error().message() << reply.error().type();
24
 
 
25
 
    m_dbus.connect("org.openobex", "/", "org.openobex.Manager", "TransferStarted", "o", this, SLOT(transferStarted(QDBusObjectPath)));
26
 
    m_dbus.connect("org.openobex", "/", "org.openobex.Manager", "TransferCompleted", "ob", this, SLOT(transferCompleted(QDBusObjectPath, bool)));
27
22
    connect(m_agent, &ObexAgent::authorized, this, &Obexd::newTransfer);
28
 
 
29
 
    QDir dir (QStandardPaths::standardLocations(QStandardPaths::DataLocation).first());
30
 
    if (!dir.exists()) {
31
 
        dir.mkpath(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first());
32
 
    }
33
23
}
34
24
 
35
25
int Obexd::rowCount(const QModelIndex &parent) const
39
29
 
40
30
QVariant Obexd::data(const QModelIndex &index, int role) const
41
31
{
42
 
    qDebug() << "asked for data" << index.row() << role << m_transfers.count();
 
32
//    qDebug() << "asked for data" << index.row() << role << m_transfers.count();
43
33
    switch(role) {
44
34
    case RoleFilename:
45
35
        return m_transfers.at(index.row())->filename();
49
39
        return m_transfers.at(index.row())->total();
50
40
    case RoleTransferred:
51
41
        return m_transfers.at(index.row())->transferred();
52
 
    case RoleCompleted:
53
 
        return m_transfers.at(index.row())->completed();
54
 
    case RoleSuccess:
55
 
        return m_transfers.at(index.row())->success();
 
42
    case RoleStatus:
 
43
        return m_transfers.at(index.row())->status();
56
44
    }
57
45
    return QVariant();
58
46
}
64
52
    roles.insert(RoleFilePath, "filePath");
65
53
    roles.insert(RoleSize, "size");
66
54
    roles.insert(RoleTransferred, "transferred");
67
 
    roles.insert(RoleCompleted, "completed");
68
 
    roles.insert(RoleSuccess, "success");
 
55
    roles.insert(RoleStatus, "status");
69
56
    return roles;
70
57
}
71
58
 
72
 
void Obexd::newTransfer(const QString &path, const QString &filePath, const QString &filename, const QString btAddress, const QString &type, int length)
 
59
void Obexd::newTransfer(const QString &path)
73
60
{
74
61
    qDebug() << "new transfer" << path;
75
 
    Transfer *t = new Transfer(path, filePath, filename, this);
 
62
 
 
63
    QString targetPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
 
64
    QDir dir(targetPath);
 
65
    if (!dir.exists()) {
 
66
        dir.mkpath(targetPath);
 
67
    }
 
68
 
 
69
    Transfer *t = new Transfer(path, targetPath, this);
76
70
    connect(t, &Transfer::totalChanged, this, &Obexd::transferProgress);
77
71
    connect(t, &Transfer::transferredChanged, this, &Obexd::transferProgress);
 
72
    connect(t, &Transfer::statusChanged, this, &Obexd::transferStatusChanged);
78
73
 
79
74
    beginInsertRows(QModelIndex(), m_transfers.count(), m_transfers.count());
80
75
    m_transfers.append(t);
81
76
    endInsertRows();
82
77
    qDebug() << "added to model";
83
 
}
84
 
 
85
 
void Obexd::transferStarted(const QDBusObjectPath &transfer)
86
 
{
87
 
    qDebug() << "transfer started";
88
 
    foreach (Transfer *t, m_transfers) {
89
 
        if (t->path() == transfer.path()) {
90
 
            t->setStarted();
91
 
            return;
92
 
        }
93
 
    }
94
 
}
95
 
 
96
 
void Obexd::transferCompleted(QDBusObjectPath transfer, bool success)
97
 
{
98
 
    foreach (Transfer *t, m_transfers) {
99
 
        if (t->path() == transfer.path()) {
100
 
            t->setCompleted(success);
101
 
            QModelIndex idx = index(m_transfers.indexOf(t));
102
 
            t->move(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first());
103
 
            emit dataChanged(idx, idx, QVector<int>() << RoleCompleted << RoleSuccess << RoleFilePath);
104
 
            return;
105
 
        }
106
 
    }
 
78
 
 
79
    // TODO: This should show pop up a question whether to accept this transfer.
 
80
    // Given we're an app and not a service at this point, let's just accept it.
 
81
    m_agent->accept(path, targetPath + "/" + t->filename());
 
82
 
107
83
}
108
84
 
109
85
void Obexd::transferProgress()
112
88
    QModelIndex idx = index(m_transfers.indexOf(t));
113
89
    emit dataChanged(idx, idx, QVector<int>() << RoleTransferred << RoleSize);
114
90
}
 
91
 
 
92
void Obexd::transferStatusChanged()
 
93
{
 
94
    Transfer *t = qobject_cast<Transfer*>(sender());
 
95
    QModelIndex idx = index(m_transfers.indexOf(t));
 
96
    emit dataChanged(idx, idx, QVector<int>() << RoleStatus);
 
97
}
 
98