Skip to content

Commit

Permalink
Allow overriding data path and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Dec 10, 2023
1 parent f9c909e commit d22240b
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 11 deletions.
15 changes: 13 additions & 2 deletions docs/backup.rst
@@ -1,3 +1,5 @@
.. _backup:

Backup
======

Expand All @@ -17,15 +19,22 @@ Back Up Manually
----------------

To back up all the data, **exit the application** first and **copy
the configuration directory**.
the configuration directory** and **the data directory**.

Path to configuration is usually:
Path to the configuration is usually:

- Windows: ``%APPDATA%\copyq``
- Portable version for Windows: ``config`` sub-folder in unzipped
application directory
- Linux: ``~/.config/copyq``

Path to the data is usually:

- Windows: ``%APPDATA%\copyq\items``
- Portable version for Windows: ``items`` sub-folder in unzipped
application directory
- Linux: ``~/.local/share/copyq/copyq/items``

To copy the configuration path to clipboard from CopyQ:

1. Open Action dialog (``F5`` shortcut).
Expand All @@ -39,6 +48,8 @@ To copy the configuration path to clipboard from CopyQ:
3. Click OK dialog button.

To copy the data path, change ``'config'`` to ``'data'``.

To restore the backup, exit the application and replace the
configuration directory.

Expand Down
2 changes: 2 additions & 0 deletions docs/faq.rst
Expand Up @@ -97,6 +97,8 @@ then choose what to import back.

Importing tabs and commands won't override existing tabs, and will create new ones.

See also: :ref:`backup`

.. _faq-disable-notifications:

How to enable or disable displaying notification when clipboard changes?
Expand Down
34 changes: 34 additions & 0 deletions docs/sessions.rst
Expand Up @@ -71,6 +71,40 @@ outside the application.
$ COPYQ_SETTINGS_PATH=$HOME/copyq-settings copyq tab
&clipboard

Item Data Path
--------------

Item data path can be overridden with ``COPYQ_ITEM_DATA_PATH``
environment variable.

::

$ copyq info data
/home/user/.local/share/copyq/copyq

$ COPYQ_ITEM_DATA_PATH=$HOME/copyq-data copyq info data
/home/user/copyq-data/copyq/copyq.conf

The directory contains data for items that exceeds 4096 bytes. The default
threshold can be overridden with ``item_data_threshold`` option.

::

$ copyq config item_data_threshold
4096

To disable using the data directory and store everything into tab data files,
set the threshold to a negative value. The tab data file will be updated only
after the items in the tab change.

::

$ copyq config item_data_threshold -1
-1

Note: Using data directory ensure that the application is fast even if there
are a lot of large items in tabs.

Icon Color
----------

Expand Down
19 changes: 19 additions & 0 deletions src/app/app.cpp
Expand Up @@ -17,6 +17,7 @@
#include <QDir>
#include <QLibraryInfo>
#include <QLocale>
#include <QStandardPaths>
#include <QTranslator>
#include <QVariant>

Expand Down Expand Up @@ -149,6 +150,24 @@ App::App(QCoreApplication *application,
QCoreApplication::setOrganizationName(session);
QCoreApplication::setApplicationName(session);

if ( qEnvironmentVariableIsEmpty("COPYQ_ITEM_DATA_PATH") ) {
if ( !m_app->property("CopyQ_item_data_path").isValid() ) {
m_app->setProperty(
"CopyQ_item_data_path",
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
+ QLatin1String("/items"));
}
} else {
m_app->setProperty(
"CopyQ_item_data_path",
#if QT_VERSION >= QT_VERSION_CHECK(5,10,0)
qEnvironmentVariable("COPYQ_ITEM_DATA_PATH")
#else
QString::fromLocal8bit(qgetenv("COPYQ_ITEM_DATA_PATH"))
#endif
);
}

#ifdef HAS_TESTS
initTests();
#endif
Expand Down
1 change: 0 additions & 1 deletion src/gui/notificationnative/notificationnative.cpp
Expand Up @@ -22,7 +22,6 @@
#include <QPainter>
#include <QPointer>
#include <QPushButton>
#include <QStandardPaths>
#include <QTextEdit>

#include <knotifications_version.h>
Expand Down
5 changes: 2 additions & 3 deletions src/item/itemstore.cpp
Expand Up @@ -13,7 +13,6 @@
#include <QFile>
#include <QSaveFile>
#include <QSet>
#include <QStandardPaths>

namespace {

Expand Down Expand Up @@ -178,8 +177,8 @@ bool moveItems(const QString &oldId, const QString &newId)

void cleanDataFiles(const QStringList &tabNames)
{
QDir dir( QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) );
if ( !dir.cd(QStringLiteral("items")) )
QDir dir(itemDataPath());
if ( !dir.exists() )
return;

QStringList files;
Expand Down
11 changes: 8 additions & 3 deletions src/item/serialize.cpp
Expand Up @@ -7,6 +7,7 @@
#include "common/mimetypes.h"

#include <QAbstractItemModel>
#include <QCoreApplication>
#include <QByteArray>
#include <QCryptographicHash>
#include <QDataStream>
Expand All @@ -15,7 +16,6 @@
#include <QList>
#include <QPair>
#include <QSaveFile>
#include <QStandardPaths>
#include <QStringList>

#include <unordered_map>
Expand Down Expand Up @@ -185,12 +185,12 @@ bool deserializeDataV2(QDataStream *out, QVariantMap *data)

QString dataFilePath(const QByteArray &bytes, bool create = false)
{
QDir dir( QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) );
QDir dir(itemDataPath());
QCryptographicHash hash(QCryptographicHash::Sha256);
hash.addData(QByteArrayLiteral("copyq_salt"));
hash.addData(bytes);
const QString sha = QString::fromUtf8( hash.result().toHex() );
const QString subpath = QStringLiteral("items/%1/%2/%3").arg(
const QString subpath = QStringLiteral("%1/%2/%3").arg(
sha.mid(0, 16),
sha.mid(16, 16),
sha.mid(32, 16)
Expand Down Expand Up @@ -432,3 +432,8 @@ bool itemDataFiles(QIODevice *file, QStringList *files)

return out.status() == QDataStream::Ok;
}

QString itemDataPath()
{
return qApp->property("CopyQ_item_data_path").toString();
}
1 change: 1 addition & 0 deletions src/item/serialize.h
Expand Up @@ -25,6 +25,7 @@ bool deserializeData(QAbstractItemModel *model, QDataStream *stream, int maxItem
bool serializeData(const QAbstractItemModel &model, QIODevice *file, int itemDataThreshold = -1);
bool deserializeData(QAbstractItemModel *model, QIODevice *file, int maxItems);

QString itemDataPath();
bool itemDataFiles(QIODevice *file, QStringList *files);

#endif // SERIALIZE_H
1 change: 1 addition & 0 deletions src/platform/win/winplatform.cpp
Expand Up @@ -125,6 +125,7 @@ Application *createApplication(int &argc, char **argv)
if ( !portableFolder.isEmpty() ) {
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, portableFolder);
qputenv("COPYQ_LOG_FILE", portableFolder.toUtf8() + "/copyq.log");
app->setProperty("CopyQ_item_data_path", portableFolder + QLatin1String("/items"));
}

return app;
Expand Down
3 changes: 1 addition & 2 deletions src/scriptable/scriptable.cpp
Expand Up @@ -47,7 +47,6 @@
#include <QSysInfo>
#include <QUrl>
#include <QVector>
#include <QStandardPaths>
#include <QThread>
#include <QTimer>

Expand Down Expand Up @@ -1741,7 +1740,7 @@ QJSValue Scriptable::info()
info.insert("config", QSettings().fileName());
info.insert("exe", QCoreApplication::applicationFilePath());
info.insert("log", logFileName());
info.insert("data", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
info.insert("data", itemDataPath());

info.insert("plugins",
#ifdef COPYQ_PLUGIN_PREFIX
Expand Down
14 changes: 14 additions & 0 deletions src/tests/tests.cpp
Expand Up @@ -3419,6 +3419,20 @@ void Tests::configPathEnvVariable()
QCOMPARE( out.left(expectedOut.size()), expectedOut );
}

void Tests::itemDataPathEnvVariable()
{
const auto path = QDir::home().absoluteFilePath("copyq-data");
const auto environment = QStringList("COPYQ_ITEM_DATA_PATH=" + path);

QByteArray out;
QByteArray err;
run(Args() << "info" << "data", &out, &err, QByteArray(), environment);
QVERIFY2( testStderr(err), err );

const auto expectedOut = path.toUtf8();
QCOMPARE( out.left(expectedOut.size()), expectedOut );
}

void Tests::configTabs()
{
const QString sep = QStringLiteral("\n");
Expand Down
1 change: 1 addition & 0 deletions src/tests/tests.h
Expand Up @@ -216,6 +216,7 @@ private slots:
void configAutostart();

void configPathEnvVariable();
void itemDataPathEnvVariable();

void configTabs();

Expand Down

0 comments on commit d22240b

Please sign in to comment.