File indexing completed on 2024-04-21 04:55:26

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #ifndef MICROBLOG_H
0010 #define MICROBLOG_H
0011 
0012 #include <QMenu>
0013 #include <QString>
0014 
0015 #include "account.h"
0016 #include "choqok_export.h"
0017 #include "choqoktypes.h"
0018 #include "choqokuiglobal.h"
0019 #include "plugin.h"
0020 
0021 class ChoqokEditAccountWidget;
0022 
0023 namespace Choqok
0024 {
0025 namespace UI
0026 {
0027 class PostWidget;
0028 class TimelineWidget;
0029 class MicroBlogWidget;
0030 class ComposerWidget;
0031 }
0032 /**
0033 \brief Base class for MicroBlog plugins
0034 Every MicroBlog plugin should subclass this they can subclass UI classes to use too, and implement:
0035 @ref createNewAccount() To create a new account for this microblog, can use @ref Choqok::Account or a subclass of it,
0036 @ref createEditAccountWidget() To create a widget/dialog to show to user for create/edit account,
0037 @ref createMicroBlogWidget() To create a @ref MicroBlogWidget to show on MainWindow
0038 @ref createTimelineWidget() To create a @ref TimelineWidget to show on MicroBlogWidget
0039 
0040 Additionally should implement this functions:
0041 @ref createPost() @ref abortCreatePost() @ref fetchPost() @ref removePost() @ref updateTimelines() @ref profileUrl()
0042 
0043 @author Mehrdad Momeny \<mehrdad.momeny@gmail.com\>
0044 */
0045 class CHOQOK_EXPORT MicroBlog : public Plugin
0046 {
0047     Q_OBJECT
0048 public:
0049     virtual ~MicroBlog();
0050 
0051     /**
0052     @brief Return a KMenu contain microblog specific actions.
0053     Can use for search facilities, or other things
0054     Will add to a button on top of @ref MicroBlogWidget of account/microblog
0055     */
0056     virtual QMenu *createActionsMenu(Account *theAccount, QWidget *parent = UI::Global::mainWindow());
0057 
0058     /**
0059     Enumeration for possible errors.
0060     */
0061     enum ErrorType {
0062         /** A server side error. */
0063         ServerError,
0064         /** An error on communication with server */
0065         CommunicationError,
0066         /** A parsing error. */
0067         ParsingError,
0068         /** An error on authentication. */
0069         AuthenticationError,
0070         /** An error where the method called is not supported by this object. */
0071         NotSupportedError,
0072         /** Any other miscellaneous error. */
0073         OtherError
0074     };
0075 
0076     /**
0077     Enumeration for levels of errors.
0078     This could use for user feedback!
0079     For @ref Critical errors will show a messagebox,
0080     For @ref Normal will show a knotify.
0081     For @ref Low just show a message in statusBar
0082     */
0083     enum ErrorLevel {
0084         Low = 0,
0085         Normal,
0086         Critical
0087     };
0088 
0089     /**
0090     * @brief Create a new Account
0091     *
0092     * This method is called during the loading of the config file.
0093     * @param alias - the alias name to create the account with.
0094     *
0095     * you don't need to register the account to the AccountManager in this function.
0096     *
0097     * @return The new @ref Account object created by this function
0098     */
0099     virtual Account *createNewAccount(const QString &alias);
0100 
0101     /**
0102     * @brief Create a new EditAccountWidget
0103     *
0104     * @return A new @ref ChoqokEditAccountWidget to be shown in the account part of the configurations.
0105     *
0106     * @param account is the Account to edit. If it's nullptr, then we create a new account
0107     * @param parent The parent of the 'to be returned' widget
0108     */
0109     virtual ChoqokEditAccountWidget *createEditAccountWidget(Choqok::Account *account, QWidget *parent) = 0;
0110 
0111     /**
0112     * @brief Create a MicroBlogWidget for this Account
0113     * The returned MicroBlogWidget will show on Mainwindow. and manage of this microblog account will give to it
0114     * Every MicroBlog plugin should reimplement this.
0115     *
0116     * @return A new MicroBlogWidget to use.
0117     *
0118     * @param account account to use.
0119     * @param parent The parent of the 'to be returned' widget
0120     */
0121     virtual UI::MicroBlogWidget *createMicroBlogWidget(Choqok::Account *account, QWidget *parent);
0122 
0123     /**
0124     * @brief Create a ComposerWidget to use in MicroBlogWidget
0125     *
0126     * @return A new ComposerWidget to use.
0127     *
0128     * @param account account to use.
0129     * @param parent The parent of the 'to be returned' widget
0130     */
0131     virtual UI::ComposerWidget *createComposerWidget(Choqok::Account *account, QWidget *parent);
0132 
0133     /**
0134     * @brief Create a TimelineWidget to use in MicroBlogWidget
0135     *
0136     * @return A new TimelineWidget to use.
0137     *
0138     * @param account account to use.
0139     * @param timelineName Name of timeline
0140     * @param parent The parent of the 'to be returned' widget
0141     */
0142     virtual UI::TimelineWidget *createTimelineWidget(Choqok::Account *account, const QString &timelineName,
0143             QWidget *parent);
0144 
0145     /**
0146      * @brief Create a PostWidget to contain one post in TimelineWidget
0147      *
0148      * @return A new PostWidget to use.
0149      *
0150      * @param account account to use.
0151      * @param post Post object.
0152      * @param parent The parent of the 'to be returned' widget
0153      */
0154     virtual UI::PostWidget *createPostWidget(Choqok::Account *account,
0155             Choqok::Post *post, QWidget *parent);
0156 
0157     /**
0158     @brief Save a specific timeline!
0159     @Note Implementation of this is optional, i.e. One microblog may don't have timeline backup
0160 
0161     @see loadTimeline()
0162     */
0163     virtual void saveTimeline(Choqok::Account *account, const QString &timelineName,
0164                               const QList<UI::PostWidget *> &timeline);
0165     /**
0166     @brief Load a specific timeline!
0167     @Note Implementation of this is optional, i.e. One microblog may don't have timeline backup
0168 
0169     @see saveTimeline()
0170     */
0171     virtual QList<Post *> loadTimeline(Choqok::Account *account, const QString &timelineName);
0172 
0173     /**
0174     \brief Create a new post
0175 
0176     @see postCreated()
0177     @see abortCreatePost()
0178     */
0179     virtual void createPost(Choqok::Account *theAccount, Choqok::Post *post);
0180 
0181     /**
0182     \brief Abort all requests!
0183     */
0184     virtual void abortAllJobs(Choqok::Account *theAccount);
0185 
0186     /**
0187     \brief Abort all createPost jobs
0188     \see abortAllJobs()
0189     */
0190     virtual void abortCreatePost(Choqok::Account *theAccount, Choqok::Post *post = nullptr);
0191     /**
0192     \brief Fetch a post
0193 
0194     @see postFetched()
0195     */
0196     virtual void fetchPost(Choqok::Account *theAccount, Choqok::Post *post);
0197 
0198     /**
0199     \brief Remove a post
0200 
0201     @see postRemoved()
0202     */
0203     virtual void removePost(Choqok::Account *theAccount, Choqok::Post *post);
0204 
0205     /**
0206     Request to update all timelines of account!
0207     They will arrive in several signals! with timelineDataReceived() signal!
0208 
0209     @see timelineDataReceived()
0210     */
0211     virtual void updateTimelines(Choqok::Account *theAccount);
0212 
0213     /**
0214     return Url to account page on service (Some kind of blog homepage)
0215     */
0216     virtual QUrl profileUrl(Choqok::Account *account, const QString &username) const;
0217 
0218     /**
0219     Provide a Web link for post with id @p postId
0220     */
0221     virtual QUrl postUrl(Choqok::Account *account, const QString &username, const QString &postId) const;
0222 
0223     /**
0224     Return a list of timelines supported by this account!
0225     It will use to show timelines! and result of timelineDataReceived() signal will be based on these!
0226 
0227     @see timelineInfo()
0228     @see timelineDataReceived()
0229     */
0230     QStringList timelineNames() const;
0231 
0232     /**
0233     Checks if @p timeline is valid for this blog! i.e. there is an entry for it at timelineTypes() list.
0234     @return True if the timeline is valid, false if not!
0235     */
0236     bool isValidTimeline(const QString &timelineName);
0237 
0238     /**
0239     @Return information about an specific timeline
0240     */
0241     virtual TimelineInfo *timelineInfo(const QString &timelineName);
0242 
0243     /**
0244     Return service homepage Url
0245     */
0246     QString homepageUrl() const;
0247 
0248     /**
0249     Returns a user readable name of blog/service type! (e.g. Identica)
0250     */
0251     QString serviceName() const;
0252 
0253     static QString errorString(ErrorType type);
0254 
0255 Q_SIGNALS:
0256 
0257     /**
0258     emit when data for a timeline received! @p type specifies the type of timeline as specifies in timelineTypes()
0259     */
0260     void timelineDataReceived(Choqok::Account *theAccount, const QString &timelineName,
0261                               QList<Choqok::Post *> data);
0262 
0263     /**
0264     emit when a post successfully created!
0265     */
0266     void postCreated(Choqok::Account *theAccount, Choqok::Post *post);
0267 
0268     /**
0269     emit when a post successfully fetched!
0270     */
0271     void postFetched(Choqok::Account *theAccount, Choqok::Post *post);
0272 
0273     /**
0274     emit when a post successfully removed!
0275     */
0276     void postRemoved(Choqok::Account *theAccount, Choqok::Post *post);
0277 
0278     /**
0279     emit when an error occurred the @p errorMessage will specify the error.
0280     */
0281     void error(Choqok::Account *theAccount, Choqok::MicroBlog::ErrorType error,
0282                const QString &errorMessage, Choqok::MicroBlog::ErrorLevel level = Normal);
0283 
0284     /**
0285     emit when an error occurred on Post manipulation. e.g. On Creation!
0286     */
0287     void errorPost(Choqok::Account *theAccount, Choqok::Post *post,
0288                    Choqok::MicroBlog::ErrorType error, const QString &errorMessage,
0289                    Choqok::MicroBlog::ErrorLevel level = Normal);
0290 
0291     /**
0292     emit when microblog plugin is going to unload, and @ref Choqok::TimelineWidget should save their timelines
0293     */
0294     void saveTimelines();
0295 
0296 protected:
0297 
0298     MicroBlog(const QString &componentName, QObject *parent = nullptr);
0299 
0300     virtual void setTimelineNames(const QStringList &);
0301     void addTimelineName(const QString &);
0302     void setServiceName(const QString &);
0303     void setServiceHomepageUrl(const QString &);
0304 
0305 protected Q_SLOTS:
0306     void slotConfigChanged();
0307 
0308 private:
0309     class Private;
0310     Private *const d;
0311 };
0312 
0313 }
0314 
0315 #endif