File indexing completed on 2024-06-23 05:18:24

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QUrl>
0010 
0011 #include <QElapsedTimer>
0012 #include <QObject>
0013 #include <QTimer>
0014 class KProcess;
0015 
0016 namespace MessageComposer
0017 {
0018 /**
0019   Starts an editor for the given URL and emits an signal when
0020   editing has been finished. Both, the editor process as well
0021   as the edited file are watched to work with as many as possible
0022   editors.
0023 */
0024 class EditorWatcher : public QObject
0025 {
0026     Q_OBJECT
0027 public:
0028     enum OpenWithOption {
0029         OpenWithDialog,
0030         NoOpenWithDialog,
0031     };
0032     enum ErrorEditorWatcher {
0033         Unknown = 0,
0034         Canceled,
0035         NoServiceFound,
0036         CannotStart,
0037         NoError,
0038     };
0039 
0040     /**
0041      * Constructs an EditorWatcher.
0042      * @param url the given URL.
0043      * @param mimeType the data MIME type.
0044      * @param option the open option.
0045      * @param parent the parent object of this EditorWatcher, which will take care of deleting
0046      *               this EditorWatcher if the parent is deleted.
0047      * @param parentWidget the parent widget of this EditorWatcher, which will be used as the parent
0048      *                     widget for message dialogs.
0049      */
0050     explicit EditorWatcher(const QUrl &url, const QString &mimeType, OpenWithOption option, QObject *parent, QWidget *parentWidget);
0051 
0052     ~EditorWatcher() override;
0053     [[nodiscard]] ErrorEditorWatcher start();
0054     [[nodiscard]] bool fileChanged() const;
0055     [[nodiscard]] QUrl url() const;
0056 Q_SIGNALS:
0057     void editDone(MessageComposer::EditorWatcher *watcher);
0058 
0059 private:
0060     void editorExited();
0061     void inotifyEvent();
0062     void checkEditDone();
0063     const QUrl mUrl;
0064     const QString mMimeType;
0065     QTimer mTimer;
0066     QElapsedTimer mEditTime;
0067 
0068     KProcess *mEditor = nullptr;
0069     QWidget *const mParentWidget;
0070 
0071     int mInotifyFd = -1;
0072     int mInotifyWatch = -1;
0073     const OpenWithOption mOpenWithOption;
0074     bool mHaveInotify = false;
0075     bool mFileOpen = false;
0076     bool mEditorRunning = false;
0077     bool mFileModified = true;
0078     bool mDone = false;
0079 };
0080 }