File indexing completed on 2024-05-12 07:51:59
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org> 0004 SPDX-FileCopyrightText: 1999 David Faure <faure@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #ifndef _KPARTS_READONLYPART_H 0010 #define _KPARTS_READONLYPART_H 0011 0012 #include <kparts/part.h> 0013 0014 #include <QUrl> 0015 0016 class KJob; 0017 namespace KIO 0018 { 0019 class Job; 0020 } 0021 0022 namespace KParts 0023 { 0024 class ReadOnlyPartPrivate; 0025 class NavigationExtension; 0026 class OpenUrlArguments; 0027 0028 /** 0029 * @class ReadOnlyPart readonlypart.h <KParts/ReadOnlyPart> 0030 * 0031 * @short Base class for any "viewer" part. 0032 * 0033 * This class takes care of network transparency for you, 0034 * in the simplest way (downloading to a temporary file, then letting the part 0035 * load from the temporary file). 0036 * To use the built-in network transparency, you only need to implement 0037 * openFile(), not openUrl(). 0038 * 0039 * To implement network transparency differently (e.g. for progressive loading, 0040 * like a web browser does for instance), or to prevent network transparency 0041 * (but why would you do that?), you can override openUrl(). 0042 * 0043 * An application using KParts can use the signals to show feedback while 0044 * the URL is being loaded. 0045 * 0046 * ReadOnlyPart handles the window caption by setting it to the current URL 0047 * (set in openUrl(), and each time the part is activated). 0048 * If you want another caption, set it in openFile() and, 0049 * if the part might ever be used with a part manager, in guiActivateEvent(). 0050 */ 0051 class KPARTS_EXPORT ReadOnlyPart : public Part 0052 { 0053 Q_OBJECT 0054 0055 Q_PROPERTY(QUrl url READ url) 0056 0057 KPARTS_DECLARE_PRIVATE(ReadOnlyPart) 0058 0059 public: 0060 /** 0061 * Constructor. 0062 * See also Part for the setXXX methods to call. 0063 */ 0064 explicit ReadOnlyPart(QObject *parent = nullptr, const KPluginMetaData &data = {}); 0065 0066 /** 0067 * Destructor. 0068 */ 0069 ~ReadOnlyPart() override; 0070 0071 /** 0072 * Call this to turn off the progress info dialog used by 0073 * the internal KIO job. Use this if you provide another way 0074 * of displaying progress info (e.g. a statusbar), using the 0075 * signals emitted by this class, and/or those emitted by 0076 * the job given by started(). 0077 */ 0078 void setProgressInfoEnabled(bool show); 0079 0080 /** 0081 * Returns whether the part shows the progress info dialog used by the internal 0082 * KIO job. 0083 */ 0084 bool isProgressInfoEnabled() const; 0085 0086 public Q_SLOTS: 0087 /** 0088 * Only reimplement this if you don't want the network transparency support 0089 * to download from the URL into a temporary file (when the URL isn't local). 0090 * Otherwise, reimplement openFile() only. 0091 * 0092 * If you reimplement it, don't forget to set the caption, usually with 0093 * @code 0094 * Q_EMIT setWindowCaption( url.toDisplayString() ); 0095 * @endcode 0096 * and also, if the URL refers to a local file, resolve it to a 0097 * local path and call setLocalFilePath(). 0098 */ 0099 virtual bool openUrl(const QUrl &url); 0100 0101 public: 0102 /** 0103 * Returns the URL currently opened in (or being opened by) this part. 0104 * @note The URL is not cleared if openUrl() fails to load the URL. 0105 * Call closeUrl() if you need to explicitly reset it. 0106 * 0107 * @return The current URL. 0108 */ 0109 QUrl url() const; 0110 0111 /** 0112 * Called when closing the current URL (for example, a document), for instance 0113 * when switching to another URL (note that openUrl() calls it 0114 * automatically in this case). 0115 * If the current URL is not fully loaded yet, aborts loading. 0116 * Deletes the temporary file used when the URL is remote. 0117 * Resets the current url() to QUrl(). 0118 * @return always true, but the return value exists for reimplementations 0119 */ 0120 virtual bool closeUrl(); 0121 0122 /** 0123 * This convenience method returns the NavigationExtension for this part, 0124 * or @c nullptr if there isn't one. 0125 */ 0126 NavigationExtension *navigationExtension() const; 0127 0128 /** 0129 * Sets the arguments to use for the next openUrl() call. 0130 */ 0131 void setArguments(const OpenUrlArguments &arguments); 0132 // TODO to avoid problems with the case where the loading fails, this could also be a openUrl() argument (heavy porting!). 0133 // However we need to have setArguments in any case for updated made by the part, see e.g. KHTMLPart::openUrl. 0134 // Well, maybe we should have setArguments (affects next openurl call) and updateArguments? 0135 0136 /** 0137 * @return the arguments that were used to open this URL. 0138 */ 0139 OpenUrlArguments arguments() const; 0140 0141 public: 0142 /** 0143 * Initiate sending data to this part. 0144 * This is an alternative to openUrl(), which allows the user of the part 0145 * to load the data itself, and send it progressively to the part. 0146 * 0147 * @param mimeType the type of data that is going to be sent to this part. 0148 * @param url the URL representing this data. Although not directly used, 0149 * every ReadOnlyPart has a URL (see url()), so this simply sets it. 0150 * @return true if the part supports progressive loading and accepts data, false otherwise. 0151 */ 0152 bool openStream(const QString &mimeType, const QUrl &url); 0153 0154 /** 0155 * Send some data to the part. openStream must have been called previously, 0156 * and must have returned true. 0157 * @return true if the data was accepted by the part. If false is returned, 0158 * the application should stop sending data, and doesn't have to call closeStream. 0159 */ 0160 bool writeStream(const QByteArray &data); 0161 0162 /** 0163 * Terminate the sending of data to the part. 0164 * With some data types (text, html...) closeStream might never actually be called, 0165 * in the case of continuous streams, for instance plain text or HTML data. 0166 */ 0167 bool closeStream(); 0168 0169 #ifdef K_DOXYGEN 0170 protected: // are parsed by doxygen (kapidox/ecm_add_qch): unhide for doxygen configured to skip private methods 0171 #else 0172 private: // Makes no sense for inherited classes to call those. But make it protected there. 0173 #endif // K_DOXYGEN 0174 0175 /** 0176 * Called by openStream to initiate sending of data. 0177 * Parts which implement progress loading should check the @p mimeType 0178 * parameter, and return true if they can accept a data stream of that type. 0179 */ 0180 virtual bool doOpenStream(const QString &mimeType) 0181 { 0182 Q_UNUSED(mimeType); 0183 return false; 0184 } 0185 /** 0186 * Receive some data from the hosting application. 0187 * In this method the part should attempt to display the data progressively. 0188 * With some data types (text, html...) closeStream might never actually be called, 0189 * in the case of continuous streams. This can't happen with e.g. images. 0190 */ 0191 virtual bool doWriteStream(const QByteArray &data) 0192 { 0193 Q_UNUSED(data); 0194 return false; 0195 } 0196 /** 0197 * This is called by closeStream(), to indicate that all the data has been sent. 0198 * Parts should ensure that all of the data is displayed at this point. 0199 * @return whether the data could be displayed correctly. 0200 */ 0201 virtual bool doCloseStream() 0202 { 0203 return false; 0204 } 0205 0206 Q_SIGNALS: 0207 /** 0208 * The part emits this when starting to load data. 0209 * If using a KIO::Job, it provides the @p job so that 0210 * progress information can be shown. Otherwise, @p job is @c nullptr. 0211 **/ 0212 void started(KIO::Job *job); 0213 0214 /** 0215 * Emit this when you have completed loading data. 0216 * Hosting applications will want to know when the process of loading the data 0217 * is finished, so that they can access the data when everything is loaded. 0218 **/ 0219 void completed(); 0220 0221 /** 0222 * This signal is similar to the @c KParts::ReadOnlyPart::completed() signal 0223 * except it is only emitted if there is still a pending action to be executed 0224 * on a delayed timer. 0225 * 0226 * An example of this is the meta-refresh tags on web pages used to reload/redirect 0227 * after a certain period of time. This signal is useful if you want to give the 0228 * user the ability to cancel such pending actions. 0229 * 0230 * @since 5.81 0231 */ 0232 void completedWithPendingAction(); 0233 0234 /** 0235 * Emit this if loading is canceled by the user or by an error. 0236 * @param errMsg the error message, empty if the user canceled the loading voluntarily. 0237 */ 0238 void canceled(const QString &errMsg); 0239 0240 /** 0241 * Emitted by the part when url() changes 0242 * @since 4.10 0243 */ 0244 void urlChanged(const QUrl &url); 0245 0246 protected: 0247 /** 0248 * If the part uses the standard implementation of openUrl(), 0249 * it must reimplement this to open the local file. 0250 * The default implementation simply returns false. 0251 * 0252 * If this method returns true the part emits completed(), 0253 * otherwise it emits canceled(). 0254 * 0255 * @see completed(), canceled() 0256 */ 0257 virtual bool openFile(); 0258 0259 /** 0260 * @internal 0261 */ 0262 void abortLoad(); 0263 0264 /** 0265 * Reimplemented from Part, so that the window caption is set to 0266 * the current URL (decoded) when the part is activated. 0267 * This is the usual behavior in 99% of applications. 0268 * Reimplement if you don't like it - test for event->activated()! 0269 * 0270 * @note This is done with GUIActivateEvent and not with 0271 * PartActivateEvent because it's handled by the main window 0272 * (which gets the event after the PartActivateEvent events have been sent). 0273 */ 0274 void guiActivateEvent(GUIActivateEvent *event) override; 0275 0276 /** 0277 * Sets the URL associated with this part. 0278 */ 0279 void setUrl(const QUrl &url); 0280 0281 /** 0282 * Returns the local file path associated with this part. 0283 * 0284 * @note The result will only be valid if openUrl() or 0285 * setLocalFilePath() has previously been called. 0286 */ 0287 QString localFilePath() const; 0288 0289 /** 0290 * Sets the local file path associated with this part. 0291 */ 0292 void setLocalFilePath(const QString &localFilePath); 0293 0294 protected: 0295 KPARTS_NO_EXPORT ReadOnlyPart(ReadOnlyPartPrivate &dd, QObject *parent); 0296 0297 private: 0298 Q_DISABLE_COPY(ReadOnlyPart) 0299 }; 0300 0301 } // namespace 0302 0303 #endif