File indexing completed on 2024-05-19 04:56:29

0001 /**
0002  * \file scriptutils.h
0003  * QML support functions.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 21 Sep 2014
0008  *
0009  * Copyright (C) 2014-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QObject>
0030 #include <QStringList>
0031 #include <QUrl>
0032 #include <QPersistentModelIndex>
0033 #include <QJSValue>
0034 #include "frame.h"
0035 
0036 /**
0037  * QML support functions.
0038  */
0039 class KID3_PLUGIN_EXPORT ScriptUtils : public QObject {
0040   Q_OBJECT
0041 public:
0042   /**
0043    * Constructor.
0044    * @param parent parent object
0045    */
0046   explicit ScriptUtils(QObject* parent = nullptr);
0047 
0048   /**
0049    * Destructor.
0050    */
0051   ~ScriptUtils() override = default;
0052 
0053   /**
0054    * Convert a list of URLs to a list of local file paths.
0055    * @param urls file URLs
0056    * @return list with local file paths.
0057    */
0058   Q_INVOKABLE static QStringList toStringList(const QList<QUrl>& urls);
0059 
0060   /**
0061    * Convert a variant list containing model indexes to a list of persistent
0062    * model indexes.
0063    * @param lst variant list with model indexes
0064    * @return persistent model index list.
0065    */
0066   Q_INVOKABLE static QList<QPersistentModelIndex> toPersistentModelIndexList(
0067       const QVariantList& lst);
0068 
0069   /**
0070    * Convert an integer to a tag version.
0071    * @param nr tag mask (0=none, 1, 2, 3=1 and 2)
0072    */
0073   Q_INVOKABLE static Frame::TagVersion toTagVersion(int nr) {
0074     return Frame::tagVersionCast(nr);
0075   }
0076 
0077   /**
0078    * Convert an integer to a tag number.
0079    * @param nr tag number
0080    */
0081   Q_INVOKABLE static Frame::TagNumber toTagNumber(int nr) {
0082     return Frame::tagNumberCast(nr);
0083   }
0084 
0085   /**
0086    * Get data for @a roleName and @a row from @a model.
0087    * @param modelObj model
0088    * @param row model row
0089    * @param roleName role name as used in scripting languages
0090    * @param parent optional parent model index
0091    * @return model data.
0092    */
0093   Q_INVOKABLE static QVariant getRoleData(QObject* modelObj, int row, const QByteArray& roleName,
0094       const QModelIndex& parent = QModelIndex());
0095 
0096   /**
0097    * Set data for @a roleName and @a row in @a model.
0098    * @param modelObj model
0099    * @param row model row
0100    * @param roleName role name as used in scripting languages
0101    * @param value model data
0102    * @param parent optional parent model index
0103    * @return true if ok.
0104    */
0105   Q_INVOKABLE static bool setRoleData(QObject* modelObj, int row,
0106       const QByteArray& roleName, const QVariant& value,
0107       const QModelIndex& parent = QModelIndex());
0108 
0109   /**
0110    * Get data for @a roleName and model @a index.
0111    * @param index model index
0112    * @param roleName role name as used in scripting languages
0113    * @return model data.
0114    */
0115   Q_INVOKABLE static QVariant getIndexRoleData(const QModelIndex& index,
0116                                                const QByteArray& roleName);
0117 
0118   /**
0119    * Get property values as a string.
0120    * @param obj object to inspect
0121    * @return string containing property values.
0122    */
0123   Q_INVOKABLE static QString properties(const QObject* obj);
0124 
0125   /**
0126    * String list of frame field ID names.
0127    */
0128   Q_INVOKABLE static QStringList getFieldIdNames();
0129 
0130   /**
0131    * String list of text encoding names.
0132    */
0133   Q_INVOKABLE static QStringList getTextEncodingNames();
0134 
0135   /**
0136    * String list of timestamp format names.
0137    */
0138   Q_INVOKABLE static QStringList getTimestampFormatNames();
0139 
0140   /**
0141    * String list of picture type names.
0142    */
0143   Q_INVOKABLE static QStringList getPictureTypeNames();
0144 
0145   /**
0146    * String list of content type names.
0147    */
0148   Q_INVOKABLE static QStringList getContentTypeNames();
0149 
0150   /**
0151    * Write data to a file.
0152    * @param filePath path to file
0153    * @param data data to write
0154    * @return true if ok.
0155    */
0156   Q_INVOKABLE static bool writeFile(const QString& filePath,
0157                                     const QByteArray& data);
0158 
0159   /**
0160    * Read data from file
0161    * @param filePath path to file
0162    * @return data read, empty if failed.
0163    */
0164   Q_INVOKABLE static QByteArray readFile(const QString& filePath);
0165 
0166   /**
0167    * Remove file.
0168    * @param filePath path to file
0169    * @return true if ok.
0170    */
0171   Q_INVOKABLE static bool removeFile(const QString& filePath);
0172 
0173   /**
0174    * Check if file exists.
0175    * @param filePath path to file
0176    * @return true if file exists.
0177    */
0178   Q_INVOKABLE static bool fileExists(const QString& filePath);
0179 
0180   /**
0181    * Check if file is writable.
0182    * @param filePath path to file
0183    * @return true if file is writable.
0184    */
0185   Q_INVOKABLE static bool fileIsWritable(const QString& filePath);
0186 
0187   /**
0188    * Get permissions of file.
0189    * @param filePath path to file
0190    * @return mode bits of file, e.g. 0x644.
0191    */
0192   Q_INVOKABLE static int getFilePermissions(const QString& filePath);
0193 
0194   /**
0195    * Set permissions of file.
0196    * @param filePath path to file
0197    * @param modeBits mode bits of file, e.g. 0x644
0198    * @return true if ok.
0199    */
0200   Q_INVOKABLE static bool setFilePermissions(const QString& filePath,
0201                                              int modeBits);
0202 
0203   /**
0204    * @brief Get type of file.
0205    * @param filePath path to file
0206    * @return "/" for directories, "@" for symlinks, "*" for executables,
0207    *         " " for files.
0208    */
0209   Q_INVOKABLE static QString classifyFile(const QString& filePath);
0210 
0211   /**
0212    * Rename file.
0213    * @param oldName old name
0214    * @param newName new name
0215    * @return true if ok.
0216    */
0217   Q_INVOKABLE static bool renameFile(const QString& oldName,
0218                                      const QString& newName);
0219 
0220   /**
0221    * Copy file.
0222    * @param source path to source file
0223    * @param dest path to destination file
0224    * @return true if ok.
0225    */
0226   Q_INVOKABLE static bool copyFile(const QString& source,
0227                                    const QString& dest);
0228 
0229   /**
0230    * Create directory.
0231    * @param path path to new directory
0232    * @return true if ok.
0233    */
0234   Q_INVOKABLE static bool makeDir(const QString& path);
0235 
0236   /**
0237    * Remove directory.
0238    * @param path path to directory to remove
0239    * @return true if ok.
0240    */
0241   Q_INVOKABLE static bool removeDir(const QString& path);
0242 
0243   /**
0244    * Get path of temporary directory.
0245    * @return temporary directory.
0246    */
0247   Q_INVOKABLE static QString tempPath();
0248 
0249   /**
0250    * Get directory containing the user's music.
0251    * @return music directory.
0252    */
0253   Q_INVOKABLE static QString musicPath();
0254 
0255   /**
0256    * Get list of currently mounted filesystems.
0257    * @return list with storage information maps containing the keys
0258    * name, displayName, isValid, isReadOnly, isReady, rootPath,
0259    * blockSize, mbytesAvailable, mbytesFree, mbytesTotal.
0260    */
0261   Q_INVOKABLE QVariantList mountedVolumes();
0262 
0263   /**
0264    * List directory entries.
0265    * @param path directory path
0266    * @param nameFilters list of name filters, e.g. ["*.jpg", "*.png"]
0267    * @param classify if true, add /, @, * for directories, symlinks, executables
0268    * @return list of directory entries.
0269    */
0270   Q_INVOKABLE static QStringList listDir(
0271       const QString& path, const QStringList& nameFilters = QStringList(),
0272       bool classify = false);
0273 
0274   /**
0275    * Synchronously start a system command.
0276    * @param program executable
0277    * @param args arguments
0278    * @param msecs timeout in milliseconds, -1 for no timeout
0279    * @return [exit code, standard output, standard error], empty list on timeout.
0280    */
0281   Q_INVOKABLE static QVariantList system(
0282       const QString& program, const QStringList& args = QStringList(),
0283       int msecs = -1);
0284 
0285   /**
0286    * Asynchronously start a system command.
0287    * @param program executable
0288    * @param args arguments
0289    * @param callback will be called with parameters [exit code, standard output,
0290    * standard error] when command finished
0291    */
0292   Q_INVOKABLE void systemAsync(
0293       const QString& program, const QStringList& args = QStringList(),
0294       QJSValue callback = QJSValue());
0295 
0296   /**
0297    * Get value of environment variable.
0298    * @param varName variable name
0299    * @return value.
0300    */
0301   Q_INVOKABLE static QByteArray getEnv(const QByteArray& varName);
0302 
0303   /**
0304    * Set value of environment variable.
0305    * @param varName variable name
0306    * @param value value to set
0307    * @return true if value could be set.
0308    */
0309   Q_INVOKABLE static bool setEnv(const QByteArray& varName,
0310                                  const QByteArray& value);
0311 
0312   /**
0313    * Get version of Kid3.
0314    * @return Kid3 version string, e.g. "3.3.0".
0315    */
0316   Q_INVOKABLE static QString getKid3Version();
0317 
0318   /**
0319    * Get release year of Kid3.
0320    * @return Kid3 year string, e.g. "2015".
0321    */
0322   Q_INVOKABLE static QString getKid3ReleaseYear();
0323 
0324   /**
0325    * Get version of Qt.
0326    * @return Qt version string, e.g. "5.4.1".
0327    */
0328   Q_INVOKABLE static QString getQtVersion();
0329 
0330   /**
0331    * Get hex string of the MD5 hash of data.
0332    * This is a replacement for Qt::md5(), which does only work with strings.
0333    * @param data data bytes
0334    * @return MD5 sum.
0335    */
0336   Q_INVOKABLE static QString getDataMd5(const QByteArray& data);
0337 
0338   /**
0339    * Get size of byte array.
0340    * @param data data bytes
0341    * @return number of bytes in @a data.
0342    */
0343   Q_INVOKABLE static int getDataSize(const QByteArray& data);
0344 
0345   /**
0346    * Create an image from data bytes.
0347    * @param data data bytes
0348    * @param format image format, default is "JPG"
0349    * @return image variant.
0350    */
0351   Q_INVOKABLE static QVariant dataToImage(const QByteArray& data,
0352                                           const QByteArray& format = "JPG");
0353 
0354   /**
0355    * Get data bytes from image.
0356    * @param var image variant
0357    * @param format image format, default is "JPG"
0358    * @return data bytes.
0359    */
0360   Q_INVOKABLE static QByteArray dataFromImage(const QVariant& var,
0361                                               const QByteArray& format = "JPG");
0362 
0363   /**
0364    * Load an image from a file.
0365    * @param filePath path to file
0366    * @return image variant.
0367    */
0368   Q_INVOKABLE static QVariant loadImage(const QString& filePath);
0369 
0370   /**
0371    * Save an image to a file.
0372    * @param var image variant
0373    * @param filePath path to file
0374    * @param format image format, default is "JPG"
0375    * @return true if ok.
0376    */
0377   Q_INVOKABLE static bool saveImage(const QVariant& var,
0378                                     const QString& filePath,
0379                                     const QByteArray& format = "JPG");
0380 
0381   /**
0382    * Get properties of an image.
0383    * @param var image variant
0384    * @return map containing "width", "height", "depth" and "colorCount",
0385    * empty if invalid image.
0386    */
0387   Q_INVOKABLE static QVariantMap imageProperties(const QVariant& var);
0388 
0389   /**
0390    * Scale an image.
0391    * @param var image variant
0392    * @param width scaled width, -1 to keep aspect ratio
0393    * @param height scaled height, -1 to keep aspect ratio
0394    * @return scaled image variant.
0395    */
0396   Q_INVOKABLE static QVariant scaleImage(const QVariant& var,
0397                                          int width, int height = -1);
0398 };