Allegati: elenco, salvataggio in Download e apertura (0.4.0)
- bridge: attachments() legge nome/lunghezza/byte come la vista originale e li tiene in cache; saveAttachment() scrive in Download con QSaveFile e scarta i percorsi nel nome scelto dal mittente - MessagePage: sezione con nome e dimensione, un tocco salva e apre - permesso SailJail Downloads nel desktop file
This commit is contained in:
1 parent
639bb9880d
commit
5e1f73774f
7 files changed
+171
-8
No files matched your search
+86
-2
@@ -2,6 +2,11 @@
|
||||
#include "accountsettings.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QSaveFile>
|
||||
#include <QStandardPaths>
|
||||
#include <QStringList>
|
||||
#include <QVariantMap>
|
||||
#include <QVector>
|
||||
@@ -21,12 +26,14 @@ gd_commit_fn EngineBridge::s_commit = nullptr;
|
||||
gd_find_start_fn EngineBridge::s_imap_start = nullptr;
|
||||
gd_find_poll_fn EngineBridge::s_imap_poll = nullptr;
|
||||
gd_rescan_fn EngineBridge::s_rescan = nullptr;
|
||||
gd_text_fn EngineBridge::s_attachments = nullptr;
|
||||
|
||||
// Colonne del motore (enum Column di qt_shim.cpp)
|
||||
enum Column { Status, Priority, Attachment, Label, Who, Date, Size, Subject, Junk };
|
||||
|
||||
EngineBridge::EngineBridge(QObject *parent)
|
||||
: QObject(parent), m_settings(nullptr), m_checking(false)
|
||||
: QObject(parent), m_settings(nullptr), m_checking(false),
|
||||
m_attFolder(-1), m_attRow(-1)
|
||||
{
|
||||
// Il controllo della posta gira in una task del motore: si interroga a
|
||||
// intervalli finché non ha finito.
|
||||
@@ -43,7 +50,7 @@ void EngineBridge::keepCallbacks(gd_folder_fn folder, gd_open_fn open, gd_cell_f
|
||||
gd_read_fn mark_read, gd_id_of_fn id_of,
|
||||
gd_move_fn move, gd_commit_fn commit,
|
||||
gd_find_start_fn imap_start, gd_find_poll_fn imap_poll,
|
||||
gd_rescan_fn rescan)
|
||||
gd_rescan_fn rescan, gd_text_fn attachments)
|
||||
{
|
||||
s_folder = folder;
|
||||
s_open = open;
|
||||
@@ -57,6 +64,7 @@ void EngineBridge::keepCallbacks(gd_folder_fn folder, gd_open_fn open, gd_cell_f
|
||||
s_imap_start = imap_start;
|
||||
s_imap_poll = imap_poll;
|
||||
s_rescan = rescan;
|
||||
s_attachments = attachments;
|
||||
if (s_instance) {
|
||||
emit s_instance->ready();
|
||||
}
|
||||
@@ -270,6 +278,82 @@ QString EngineBridge::body(int folder, int row)
|
||||
return QString::fromUtf8(buf, n);
|
||||
}
|
||||
|
||||
// Gli allegati del messaggio, come li dà il motore: per ognuno nome (UTF-8),
|
||||
// NUL, la lunghezza, NUL, i byte. Si leggono una volta sola e si tengono,
|
||||
// perché ogni chiamata li ricostruisce da capo (fino a 80 MB).
|
||||
QVariantList EngineBridge::attachments(int folder, int row)
|
||||
{
|
||||
QVariantList out;
|
||||
if (!s_attachments || !s_id_of) return out;
|
||||
const int id = s_id_of(folder, row);
|
||||
if (id < 0) return out;
|
||||
|
||||
QByteArray blob(80 << 20, Qt::Uninitialized);
|
||||
const int n = s_attachments(folder, id, blob.data(), blob.size());
|
||||
if (n <= 0) return out;
|
||||
blob.truncate(n);
|
||||
|
||||
m_attBytes.clear();
|
||||
m_attNames.clear();
|
||||
m_attFolder = folder;
|
||||
m_attRow = row;
|
||||
|
||||
int at = 0;
|
||||
while (at < blob.size()) {
|
||||
const int a = blob.indexOf('\0', at);
|
||||
if (a < 0) break;
|
||||
const int b = blob.indexOf('\0', a + 1);
|
||||
if (b < 0) break;
|
||||
bool ok = false;
|
||||
const int length = blob.mid(a + 1, b - a - 1).toInt(&ok);
|
||||
if (!ok || length < 0 || b + 1 + length > blob.size()) break;
|
||||
|
||||
const QString nome = QString::fromUtf8(blob.mid(at, a - at));
|
||||
const QByteArray bytes = blob.mid(b + 1, length);
|
||||
m_attNames.append(nome);
|
||||
m_attBytes.append(bytes);
|
||||
|
||||
QVariantMap m;
|
||||
m["nome"] = nome;
|
||||
m["dimensione"] = bytes.size();
|
||||
m["indice"] = out.size();
|
||||
out.append(m);
|
||||
at = b + 1 + length;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Salva un allegato in Download e restituisce il percorso ("" se non riesce).
|
||||
QString EngineBridge::saveAttachment(int folder, int row, int index)
|
||||
{
|
||||
if (folder != m_attFolder || row != m_attRow)
|
||||
attachments(folder, row);
|
||||
if (index < 0 || index >= m_attBytes.size()) return QString();
|
||||
|
||||
QString dir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
||||
if (dir.isEmpty()) dir = QDir::homePath() + QStringLiteral("/Downloads");
|
||||
QDir().mkpath(dir);
|
||||
|
||||
// il nome arriva dal mittente: si tiene solo l'ultimo pezzo, senza percorsi
|
||||
QString nome = QFileInfo(m_attNames.at(index)).fileName();
|
||||
if (nome.isEmpty()) nome = QStringLiteral("allegato-%1").arg(index + 1);
|
||||
QString path = dir + "/" + nome;
|
||||
const QFileInfo fi(nome);
|
||||
for (int k = 1; QFile::exists(path) && k < 1000; k++) {
|
||||
path = fi.suffix().isEmpty()
|
||||
? QString("%1/%2 (%3)").arg(dir, nome, QString::number(k))
|
||||
: QString("%1/%2 (%3).%4").arg(dir, fi.completeBaseName(),
|
||||
QString::number(k), fi.suffix());
|
||||
}
|
||||
|
||||
QSaveFile f(path);
|
||||
if (!f.open(QIODevice::WriteOnly)) return QString();
|
||||
const QByteArray &bytes = m_attBytes.at(index);
|
||||
if (f.write(bytes) != bytes.size()) { f.cancelWriting(); return QString(); }
|
||||
if (!f.commit()) return QString();
|
||||
return path;
|
||||
}
|
||||
|
||||
int EngineBridge::totalUnread()
|
||||
{
|
||||
if (!s_folder || !s_unread) return 0;
|
||||
|
||||
+10
-1
@@ -33,7 +33,7 @@ public:
|
||||
gd_read_fn mark_read, gd_id_of_fn id_of,
|
||||
gd_move_fn move, gd_commit_fn commit,
|
||||
gd_find_start_fn imap_start, gd_find_poll_fn imap_poll,
|
||||
gd_rescan_fn rescan);
|
||||
gd_rescan_fn rescan, gd_text_fn attachments);
|
||||
|
||||
bool checking() const { return m_checking; }
|
||||
QString lastResult() const { return m_lastResult; }
|
||||
@@ -46,6 +46,8 @@ public:
|
||||
Q_INVOKABLE void markRead(int folder, int row, bool read);
|
||||
Q_INVOKABLE bool moveToTrash(int folder, int row);
|
||||
Q_INVOKABLE void checkMail();
|
||||
Q_INVOKABLE QVariantList attachments(int folder, int row);
|
||||
Q_INVOKABLE QString saveAttachment(int folder, int row, int index);
|
||||
|
||||
signals:
|
||||
void ready();
|
||||
@@ -69,11 +71,18 @@ private:
|
||||
static gd_find_start_fn s_imap_start;
|
||||
static gd_find_poll_fn s_imap_poll;
|
||||
static gd_rescan_fn s_rescan;
|
||||
static gd_text_fn s_attachments;
|
||||
|
||||
AccountSettings *m_settings;
|
||||
QTimer *m_poll;
|
||||
QString m_lastResult;
|
||||
bool m_checking;
|
||||
// Gli allegati dell'ultimo messaggio aperto: letti una volta sola, perché
|
||||
// il motore li ricostruisce da capo a ogni chiamata.
|
||||
QList<QByteArray> m_attBytes;
|
||||
QStringList m_attNames;
|
||||
int m_attFolder;
|
||||
int m_attRow;
|
||||
};
|
||||
|
||||
#endif // ENGINEBRIDGE_H
|
||||
@@ -59,7 +59,7 @@ int gd_run(gd_folder_fn folder, gd_open_fn open, gd_cell_fn cell,
|
||||
|
||||
EngineBridge::keepCallbacks(folder, open, cell, body, unread,
|
||||
mark_read, id_of, move, commit,
|
||||
imap_start, imap_poll, rescan);
|
||||
imap_start, imap_poll, rescan, attachments);
|
||||
return QCoreApplication::exec();
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -21,9 +21,9 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||
|
||||
app->setOrganizationName(QStringLiteral("harbour"));
|
||||
app->setApplicationName(QStringLiteral("grumpydove"));
|
||||
app->setApplicationVersion(QStringLiteral("0.3.4"));
|
||||
app->setApplicationVersion(QStringLiteral("0.4.0"));
|
||||
|
||||
qDebug() << "harbour-grumpydove v0.3.4 build" << __DATE__ << __TIME__;
|
||||
qDebug() << "harbour-grumpydove v0.4.0 build" << __DATE__ << __TIME__;
|
||||
|
||||
QTranslator translator;
|
||||
if (translator.load(QLocale(), QStringLiteral("harbour-grumpydove"),
|
||||
|
||||
Reference in new issue
Block a user