Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
refactor: use json communication.
Browse files Browse the repository at this point in the history
Change-Id: Ie25215b8d048f5726f4eaabbb666f407d65da4fd
  • Loading branch information
reionwong committed Feb 24, 2018
1 parent efbfbef commit 68726db
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 33 deletions.
1 change: 1 addition & 0 deletions deepin-font-installer/listitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ListItem : public DSimpleListItem
void setStatus(Status status);
void drawBackground(QRect rect, QPainter *painter, int index, bool isSelect);
void drawForeground(QRect rect, QPainter *painter, int column, int index, bool isSelect);
bool isInstalled() { return m_fontInfo; }

private:
DFontInfo *m_fontInfo;
Expand Down
11 changes: 1 addition & 10 deletions deepin-font-installer/multifilepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void MultiFilePage::batchInstallation()
m_fontManager->start();
}

void MultiFilePage::onProgressChanged(const QString &filePath, const float &percent)
void MultiFilePage::onProgressChanged(const QString &filePath, const double &percent)
{
m_installBtn->setVisible(false);
m_viewFileBtn->setVisible(false);
Expand All @@ -156,15 +156,6 @@ void MultiFilePage::onProgressChanged(const QString &filePath, const float &perc
item->setStatus(ListItem::Installed);
m_listView->update();

// int nextIndex = m_listWidget->row(item->getItem()) + 1;
// if (nextIndex < m_listWidget->count()) {
// QListWidgetItem *item = m_listWidget->item(nextIndex);
// ListItem *nextItem = qobject_cast<ListItem *>(m_listWidget->itemWidget(item));

// nextItem->setStatus(ListItem::Installing);
// m_listWidget->scrollToItem(nextItem->getItem());
// }

if (percent == 100) {
onWorkerFinished();
}
Expand Down
2 changes: 1 addition & 1 deletion deepin-font-installer/multifilepage.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MultiFilePage : public QWidget
private slots:
void refreshList();
void batchInstallation();
void onProgressChanged(const QString &filePath, const float &percent);
void onProgressChanged(const QString &filePath, const double &percent);
void onWorkerFinished();
void onViewFileBtnClicked();

Expand Down
2 changes: 1 addition & 1 deletion deepin-font-installer/singlefilepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void SingleFilePage::updateInfo(DFontInfo *info)
m_typeLabel->setText(m_fontInfo->type);
m_versionLabel->setText(fm.elidedText(m_fontInfo->version, Qt::ElideRight, 300));
m_copyrightLabel->setText(fm.elidedText(m_fontInfo->copyright, Qt::ElideRight, 700));
m_descriptionLabel->setText(fm.elidedText(m_fontInfo->description, Qt::ElideRight, 720));
m_descriptionLabel->setText(fm.elidedText(m_fontInfo->description, Qt::ElideRight, 710));
}

void SingleFilePage::refreshPage()
Expand Down
3 changes: 2 additions & 1 deletion dfont-install/dfont-install.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ QT -= gui
CONFIG += c++11

TARGET = dfont-install
CONFIG += console
CONFIG += console link_pkgconfig
CONFIG -= app_bundle
PKGCONFIG += fontconfig

TEMPLATE = app

Expand Down
59 changes: 51 additions & 8 deletions dfont-install/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCryptographicHash>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QFileInfo>
#include <QProcess>
#include <QDir>
#include <QDebug>
#include <iostream>
#include "dfontinfomanager.h"
#include <fontconfig/fontconfig.h>

inline QString dataToMd5Hex(const QByteArray &data)
{
Expand All @@ -42,6 +45,41 @@ inline void checkDirectory()
}
}

inline bool FcAddAppFont(const char * filepath)
{
FcInit();
bool result = FcConfigAppFontAddFile(NULL, (FcChar8 *) filepath);
return result;
}

inline bool FcAddAppFontDir(const char * dir)
{
FcInit();
bool result = FcConfigAppFontAddDir(NULL, (FcChar8 *) dir);
return result;
}

inline bool FcLoadConfig(const char * filepath)
{
FcInit();
bool result = FcConfigParseAndLoad(FcConfigGetCurrent(), (FcChar8 *) filepath, FcFalse);
return result;
}

inline bool FcCacheUpdate(void)
{
FcInit();
FcConfigDestroy(FcConfigGetCurrent());
return !FcConfigUptoDate(NULL) && FcInitReinitialize();
}

inline bool FcEnableUserConfig(bool enable)
{
FcInit();
bool result = FcConfigEnableHome(enable);
return result;
}

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Expand All @@ -56,15 +94,15 @@ int main(int argc, char *argv[])
QString targetDir = "";

for (const QString file : fileList) {
QProcess process;
DFontInfo *fontInfo = fontInfoManager->getFontInfo(file);
QProcess *process = new QProcess;
const bool isInstalled = fontInfo->isInstalled;

if (isInstalled) {
const QString sysPath = fontInfoManager->getInstalledFontPath(fontInfo);
target = sysPath;
process->start("cp", QStringList() << "-f" << file << sysPath);
process->waitForFinished(-1);
process.start("cp", QStringList() << "-f" << file << sysPath);
process.waitForFinished(-1);
} else {
const QFileInfo info(file);
QString dirName = fontInfo->familyName;
Expand All @@ -87,18 +125,23 @@ int main(int argc, char *argv[])
QFile::copy(file, target);
}

process->start("fc-cache", QStringList() << "-v" << target);
process->waitForFinished(-1);
process->deleteLater();
FcCacheUpdate();

const int currentIndex = fileList.indexOf(file);
const int count = fileList.count() - 1;

if (fileList.count() == 1) {
std::cout << target.toUtf8().data() << std::endl;
} else {
const QString output = QString("%1:%2").arg(file, QString::number(currentIndex / float(count) * 100));
std::cout << output.toUtf8().data() << std::endl;
QJsonObject object;
object.insert("FilePath", file);
object.insert("Percent", currentIndex / double(count) * 100);

QJsonDocument document;
document.setObject(object);
QByteArray array = document.toJson(QJsonDocument::Compact);

std::cout << array.data() << std::endl;
}
}

Expand Down
30 changes: 19 additions & 11 deletions libdeepin-font-installer/dfontmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include "dfontmanager.h"
#include <QProcess>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

static DFontManager *INSTANCE = 0;

Expand Down Expand Up @@ -89,18 +92,23 @@ bool DFontManager::doCmd(const QString &program, const QStringList &arguments)
bool failed = false;

if (m_type == Install) {
connect(process, &QProcess::readyReadStandardOutput, this, [&] {
const QString output = process->readAllStandardOutput();

if (m_instFileList.count() == 1) {
emit installChanged(output);
} else {
const QStringList items = output.split(QChar(':'));
emit installing(items.first(), items.last().toFloat());
}
});
connect(process, &QProcess::readyReadStandardOutput, this,
[&] {
const QString output = process->readAllStandardOutput();

// single file installation.
if (m_instFileList.count() == 1) {
emit installChanged(output);
} else {
QJsonDocument document = QJsonDocument::fromJson(output.toUtf8());
QJsonObject object = document.object();
emit installing(object.value("FilePath").toString(),
object.value("Percent").toDouble());
}
});
} else {
connect(process, &QProcess::readyReadStandardOutput, this, [&] { emit output(process->readAllStandardOutput()); });
connect(process, &QProcess::readyReadStandardOutput, this,
[&] { emit output(process->readAllStandardOutput()); });
}

process->start(program, arguments);
Expand Down
2 changes: 1 addition & 1 deletion libdeepin-font-installer/dfontmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DFontManager : public QThread

signals:
void output(const QString &output);
void installing(const QString &filePath, const float &percent);
void installing(const QString &filePath, const double &percent);
void singleInstallFinished();
void installFinished();
void installChanged(const QString &instPath);
Expand Down

0 comments on commit 68726db

Please sign in to comment.