1 /*
2 This file is part of Choqok, the KDE micro-blogging client
3 
4 Copyright (C) 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see http://www.gnu.org/licenses/
21 */
22 #ifndef POSTWIDGET_H
23 #define POSTWIDGET_H
24 
25 #include <QDateTime>
26 
27 #include "account.h"
28 #include "choqoktypes.h"
29 #include "microblog.h"
30 
31 class QAction;
32 class QPushButton;
33 
34 namespace Choqok
35 {
36 namespace UI
37 {
38 
39 class TextBrowser;
40 
41 /**
42 Post Widget!
43 Attribute "Qt::WA_DeleteOnClose" is enabled at construtor! So please use close() for deleting an object, instead of deleteLater() or delete
44 
45 @author Mehrdad Momeny \<mehrdad.momeny@gmail.com\>
46 */
47 class CHOQOK_EXPORT PostWidget : public QWidget
48 {
49     Q_OBJECT
50     Q_PROPERTY(bool read READ isRead)
51 public:
52     explicit PostWidget(Choqok::Account *account, Choqok::Post *post, QWidget *parent = nullptr);
53     virtual ~PostWidget();
54     Post *currentPost() const;
55     virtual void setRead(bool read = true);
56 
57     /**
58      Sets post widget as read, and emits postReaded() signal
59      */
60     void setReadWithSignal();
61 
62     virtual bool isRead() const;
63 
64     Account *currentAccount();
65     /**
66     Setup UI elements
67     @Note Should call from outside of class, To initialize drived class items too!
68     */
69     virtual void initUi();
70 
71     /**
72     Set stylesheet data with new color data! to use later.
73 
74     @see setUiStyle()
75     */
76     static void setStyle(const QColor &unreadColor, const QColor &unreadBack,
77                          const QColor &readColor, const QColor &readBack,
78                          const QColor &ownColor, const QColor &ownBack,
79                          const QFont &font);
80 
81     /**
82     @brief Set current post
83 
84     @note Use with care!
85 
86     After changing current post, Don't forget to call @ref initUi() to update post UI.
87     */
88     void setCurrentPost(Post *post);
89 
90     /**
91     @brief Sets Post sign
92     sign is the text that showed as sign of this post under post content.
93     */
94     void setSign(const QString &sign);
95 
96     /**
97     @return post sign
98     sign is an html text that showed as sign of this post under post content.
99     */
100     QString sign() const;
101 
102     /**
103     @brief Sets post content
104     Post content is an html text that showed as post text.
105     */
106     void setContent(const QString &content);
107 
108     /**
109     @return post content
110     Post content is an html text that showed as post text.
111     */
112     QString content() const;
113 
114     void deleteLater();
115 
116     TextBrowser *mainWidget();
117 
118     QStringList urls();
119 
120     TimelineWidget *timelineWidget() const;
121 
122     /**
123      * Plugins can add status specific actions and process them internally
124      *
125      */
126     static void addAction(QAction *action);
127 
128     static QString getBaseStyle();
129 
130 public Q_SLOTS:
131     /**
132     Set Style sheet of widget to corresponding data->
133     @see setStyle()
134     */
135     void setUiStyle();
136 
137 Q_SIGNALS:
138     /**
139     Emit and contain text to resend.
140     */
141     void resendPost(const QString &text);
142     /**
143     Emit when this post has been readed by pressing mouse on it, And to notify TimelineWidget about it.
144     */
145     void postReaded();
146     /**
147     Carry reply information, to reply to a post.
148     */
149     void reply(const QString &txt, const QString &replyToId, const QString &replyToUsername);
150 
151     /**
152     Emitted when this widget is about to close!
153     postId and this returned!
154     */
155     void aboutClosing(const QString &postId, PostWidget *widget);
156 
157 protected Q_SLOTS:
158 
159     virtual void checkAnchor(const QUrl &url);
160     /**
161     Set height of widget related to text contents
162     */
163     virtual void setHeight();
164 
165     /**
166     Update UI after changes, such as timestamp
167     */
168     virtual void updateUi();
169 
170     /**
171     Call microblog() to remove this post!
172     */
173     virtual void removeCurrentPost();
174     /**
175     Prepare text to send for resending this post.
176     */
177     virtual void slotResendPost();
178     /**
179     Internal slot to remove/close/destroy this post after bing deleted
180     */
181     void slotCurrentPostRemoved(Choqok::Account *theAccount, Choqok::Post *post);
182 
183     virtual void slotPostError(Choqok::Account *theAccount, Choqok::Post *post,
184                                Choqok::MicroBlog::ErrorType error, const QString &errorMessage);
185 
186     void avatarFetchError(const QUrl &remoteUrl, const QString &errMsg);
187     void avatarFetched(const QUrl &remoteUrl, const QPixmap &pixmap);
188 
189     void slotImageFetched(const QUrl &remoteUrl, const QPixmap &pixmap);
190     virtual void mousePressEvent(QMouseEvent *ev) override;
191 
192 protected:
193     virtual void setupUi();
194     virtual void closeEvent(QCloseEvent *event) override;
195     virtual void setupAvatar();
196     virtual void fetchImage();
197     virtual void wheelEvent(QWheelEvent *) override;
198     virtual void resizeEvent(QResizeEvent *event) override;
199     virtual void enterEvent(QEvent *event) override;
200     virtual void leaveEvent(QEvent *event) override;
201     virtual QString prepareStatus(const QString &text);
202     QLatin1String getDirection(QString text);
203     virtual QString generateSign();
204     virtual QString formatDateTime(const QDateTime &time);
205     virtual bool isResendAvailable() ;
206     virtual bool isRemoveAvailable() ;
207     virtual bool isOwnPost();
208     virtual QString removeTags(const QString &text) const;
209     /**
210     @brief Create and Add a new button to widget
211     This function will add button to UI!
212     @return added button, for some managements such as connect to a slot
213     */
214     QPushButton *addButton(const QString &objName, const QString &toolTip, const QString &icon);
215     QPushButton *addButton(const QString &objName, const QString &toolTip, const QIcon &icon);
216     QMap<QString, QPushButton *> &buttons();
217 
218     QString getUsernameHyperlink(const Choqok::User &user) const;
219 
220 protected:
221     TextBrowser *_mainWidget;
222     static const QString baseStyle;
223     static QString readStyle;
224     static QString unreadStyle;
225     static QString ownStyle;
226     static const QString webIconText;
227     static const QString hrefTemplate;
228     static const QString baseTextTemplate;
229     static const QRegExp dirRegExp;
230 
231     void setAvatarText(const QString &text);
232     QString avatarText() const;
233     void setExtraContents(const QString &text);
234     QString extraContents() const;
235     virtual QString generateResendText();
236     void updatePostImage(int width);
237 
238 private:
239     class Private;
240     Private *const d;
241 };
242 
243 class CHOQOK_EXPORT PostWidgetUserData : public QObjectUserData
244 {
245 public:
246     PostWidgetUserData(PostWidget *postWidget);
247     virtual ~PostWidgetUserData();
248     PostWidget *postWidget();
249     void setPostWidget(PostWidget *widget);
250 
251 private:
252     class Private;
253     Private *const d;
254 };
255 
256 }
257 }
258 #endif // POSTWIDGET_H
259