File indexing completed on 2025-01-26 05:08:07

0001 #pragma once
0002 
0003 /*
0004  * SPDX-FileCopyrightText: 2003-2007 Craig Drummond <craig@kde.org>
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "JobRunner.h"
0009 #include "Misc.h"
0010 #include <QDialog>
0011 #include <QThread>
0012 #include <QTreeWidget>
0013 
0014 class QLabel;
0015 class QMenu;
0016 class QAction;
0017 class QDialogButtonBox;
0018 class QAbstractButton;
0019 
0020 namespace KFI
0021 {
0022 class CActionLabel;
0023 class CFontList;
0024 class CDuplicatesDialog;
0025 
0026 class CFontFileList : public QThread
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     typedef QHash<Misc::TFont, QSet<QString>> TFontMap;
0032 
0033     //
0034     // TFile store link from filename to FontMap item.
0035     // This is used when looking for duplicate filenames (a.ttf/a.TTF).
0036     struct TFile {
0037         TFile(const QString &n, CFontFileList::TFontMap::Iterator i)
0038             : name(n)
0039             , it(i)
0040             , useLower(false)
0041         {
0042         }
0043         TFile(const QString &n, bool l = false)
0044             : name(n)
0045             , useLower(l)
0046         {
0047         }
0048 
0049         bool operator==(const TFile &f) const
0050         {
0051             return (useLower || f.useLower) ? (name.compare(f.name, Qt::CaseInsensitive) == 0) : (name == f.name);
0052         }
0053 
0054         QString name;
0055         CFontFileList::TFontMap::Iterator it;
0056         bool useLower;
0057     };
0058 
0059 public:
0060     CFontFileList(CDuplicatesDialog *parent);
0061 
0062     void start();
0063     void terminate();
0064     void getDuplicateFonts(TFontMap &map);
0065     bool wasTerminated() const
0066     {
0067         return m_terminated;
0068     }
0069 
0070 Q_SIGNALS:
0071 
0072     void finished();
0073 
0074 private:
0075     void run() override;
0076     void fileDuplicates(const QString &folder, const QSet<TFile> &files);
0077 
0078 private:
0079     bool m_terminated;
0080     TFontMap m_map;
0081 };
0082 
0083 class CFontFileListView : public QTreeWidget
0084 {
0085     Q_OBJECT
0086 
0087 public:
0088     class StyleItem : public QTreeWidgetItem
0089     {
0090     public:
0091         StyleItem(CFontFileListView *parent, const QStringList &details, const QString &fam, quint32 val)
0092             : QTreeWidgetItem(parent, details)
0093             , m_family(fam)
0094             , m_value(val)
0095         {
0096         }
0097 
0098         const QString &family() const
0099         {
0100             return m_family;
0101         }
0102         quint32 value() const
0103         {
0104             return m_value;
0105         }
0106 
0107     private:
0108         QString m_family;
0109         quint32 m_value;
0110     };
0111 
0112     CFontFileListView(QWidget *parent);
0113     ~CFontFileListView() override
0114     {
0115     }
0116 
0117     QSet<QString> getMarkedFiles();
0118     CJobRunner::ItemList getMarkedItems();
0119     void removeFiles();
0120 
0121 Q_SIGNALS:
0122 
0123     void haveDeletions(bool have);
0124 
0125 private Q_SLOTS:
0126 
0127     void openViewer();
0128     void properties();
0129     void mark();
0130     void unmark();
0131     void selectionChanged();
0132     void clicked(QTreeWidgetItem *item, int col);
0133     void contextMenuEvent(QContextMenuEvent *ev) override;
0134 
0135 private:
0136     void checkFiles();
0137 
0138 private:
0139     QMenu *m_menu;
0140     QAction *m_markAct, *m_unMarkAct;
0141 };
0142 
0143 class CDuplicatesDialog : public QDialog
0144 {
0145     Q_OBJECT
0146 
0147 public:
0148     CDuplicatesDialog(QWidget *parent, CFontList *fl);
0149 
0150     int exec() override;
0151     const CFontList *fontList() const
0152     {
0153         return m_fontList;
0154     }
0155 
0156 private Q_SLOTS:
0157 
0158     void scanFinished();
0159     void slotButtonClicked(QAbstractButton *button);
0160     void enableButtonOk(bool);
0161 
0162 private:
0163     QDialogButtonBox *m_buttonBox;
0164     CActionLabel *m_actionLabel;
0165     CFontFileList *m_fontFileList;
0166     QLabel *m_label;
0167     CFontFileListView *m_view;
0168     CFontList *m_fontList;
0169 };
0170 
0171 }