File: minorfeeditem.cpp

package info (click to toggle)
dianara 1.3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,896 kB
  • ctags: 2,005
  • sloc: cpp: 22,181; xml: 34; makefile: 3
file content (517 lines) | stat: -rw-r--r-- 17,452 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
 *   This file is part of Dianara
 *   Copyright 2012-2016  JanKusanagi JRR <jancoding@gmx.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .
 */

#include "minorfeeditem.h"


MinorFeedItem::MinorFeedItem(ASActivity *activity,
                             bool highlightedByFilter,
                             PumpController *pumpController,
                             GlobalObject *globalObject,
                             QWidget *parent) : QFrame(parent)
{
    this->pController = pumpController;
    this->globalObj = globalObject;

    this->itemIsNew = false;
    this->haveTargetAvatar = false;

    // This sizePolicy prevents cut messages, and the huge space at the end
    // of the feed, after clicking "Older Activities" several times
    this->setSizePolicy(QSizePolicy::Minimum,
                        QSizePolicy::Preferred);

    activity->setParent(this); // reparent the passed activity

    // Store activity ID so MinorFeed can reconstruct "next" url's
    this->activityId = activity->getId();

    QFont mainFont;
    mainFont.setPointSize(mainFont.pointSize() - 2);

    QString authorId = activity->author()->getId();

    avatarButton = new AvatarButton(activity->author(),
                                    this->pController,
                                    this->globalObj,
                                    this->globalObj->getMfAvatarSize(),
                                    this);


    createdAtTimestamp = activity->getCreatedAt();
    QString timestampTooltip = "<b>"
                               + Timestamp::localTimeDate(createdAtTimestamp)
                               + "</b>";

    QString generator = activity->getGenerator();
    if (!generator.isEmpty())
    {
        timestampTooltip.append("<br>"
                                + tr("Using %1",
                                     "Application used to generate this activity")
                                  .arg(generator));
    }

    QString toField = activity->getToString();
    QString ccField = activity->getCCString();
    if (!toField.isEmpty() || !ccField.isEmpty())
    {
        timestampTooltip.append("<br>"); // Extra separation before To or Cc

        if (!toField.isEmpty())
        {
            timestampTooltip.append("<br>"
                                    + tr("To: %1",
                                         "1=people to whom this activity was sent")
                                      .arg(toField));
        }
        if (!ccField.isEmpty())
        {
            timestampTooltip.append("<br>"
                                    + tr("Cc: %1",
                                         "1=people to whom this activity was sent as CC")
                                      .arg(ccField));
        }
    }

    timestampLabel = new QLabel(this);
    mainFont.setBold(true);
    timestampLabel->setFont(mainFont);
    timestampLabel->setWordWrap(true);
    timestampLabel->setToolTip(timestampTooltip.trimmed());
    timestampLabel->setAlignment(Qt::AlignCenter);
    timestampLabel->setAutoFillBackground(true);
    timestampLabel->setForegroundRole(QPalette::Text);
    timestampLabel->setBackgroundRole(QPalette::Base);
    timestampLabel->setFrameStyle(QFrame::Panel | QFrame::Raised);
    this->setFuzzyTimeStamp();


    // The description itself, like "JaneDoe updated a note",
    activityDescriptionLabel = new QLabel(this); // To be filled later
    QFont descriptionFont;
    descriptionFont.fromString(globalObj->getMinorFeedFont());
    activityDescriptionLabel->setFont(descriptionFont);
    activityDescriptionLabel->setWordWrap(true);
    activityDescriptionLabel->setOpenExternalLinks(true);
    activityDescriptionLabel->setAlignment(Qt::AlignTop);
    activityDescriptionLabel->setSizePolicy(QSizePolicy::Ignored,
                                            QSizePolicy::MinimumExpanding);
    connect(activityDescriptionLabel, SIGNAL(linkHovered(QString)),
            this, SLOT(showUrlInfo(QString)));

    // Tooltip contents
    QString activityTooltip = activity->generateTooltip();
    if (!activityTooltip.isEmpty())
    {
        // Set the activity info as tooltip for description label
        this->activityDescriptionLabel->setToolTip(activityTooltip);

        // Set a different mouse cursor for the description label, as a hint to wait for tooltip info
        this->activityDescriptionLabel->setCursor(Qt::WhatsThisCursor);
    }


    originalObjectMap = activity->object()->getOriginalObject();

    inReplyToMap = activity->object()->getInReplyTo();
    QString inReplyToAuthorId = ASPerson::cleanupId(inReplyToMap.value("author").toMap()
                                                                .value("id").toString());



    // Highlight this item if it's about the user, with different colors
    itemHighlightType = -1; // False

    // We are in the recipient list of the activity
    if (activity->getRecipientsIdList().contains(pController->currentUserId()))
    {
        itemHighlightType = 0;
    }

    // Activity is in reply to something made by us
    if (inReplyToAuthorId == pController->currentUserId())
    {
        itemHighlightType = 1;
    }

    // We are the object; someone added us, etc.
    if (activity->object()->getId() == pController->currentUserId())
    {
        itemHighlightType = 2;
    }

    // The object is ours; someone favorited our note, or something
    if (activity->object()->author()->getId() == pController->currentUserId())
    {
        itemHighlightType = 3;
    }

    //// Special case: highlighting the item because there's a filter rule for it
    if (highlightedByFilter && itemHighlightType == -1) // Only if there's no other reason for HL
    {
        itemHighlightType = 4;
    }


    if (itemHighlightType != -1)
    {
        // Unless the user ID is also empty!
        if (!pController->currentUserId().isEmpty())
        {
            QString highlightItemColor = this->globalObj->getColor(itemHighlightType);

            if (QColor::isValidColor(highlightItemColor)) // Valid color
            {
                // CSS for horizontal gradient from configured color to transparent
                QString css = QString("MinorFeedItem "
                                      "{ background-color: "
                                      "qlineargradient(spread:pad, "
                                      "x1:0, y1:0, x2:1, y2:0, "
                                      "stop:0 %1, stop:1 rgba(0, 0, 0, 0)); "
                                      "}")
                              .arg(highlightItemColor);

                this->setStyleSheet(css);
            }
            else // If there's no valid color, highlight with a border
            {
                this->setFrameStyle(QFrame::Panel);
            }
        }
    }


    // Set the activity description with optional snippet
    QString activityDescription = activity->getContent();


    // Add a snippet if configured to do so
    if (this->globalObj->getMinorFeedSnippetsType() != 3) // 3=Never
    {
        if (this->globalObj->getMinorFeedSnippetsType() == 2 // 2=Always
         || this->itemHighlightType != -1)       // 0/1/2 and it's highlighted
        {
            if (this->globalObj->getMinorFeedSnippetsType() > 0  // Always or any highlighted
             || authorId != pController->currentUserId())        // or highlighted but not ours (0)
            {
                // FIXME 1.3.6: fix this spaghetti mess!!
                // TODO: option to not show when the object of the activity is ours
                // i.e. "JohnDoe liked a note", our note

                int snippetCharLimit;
                if (this->itemHighlightType == -1)
                {
                    snippetCharLimit = globalObj->getSnippetsCharLimit();
                }
                else
                {
                    snippetCharLimit = globalObj->getSnippetsCharLimitHl();
                }

                QString snippet = activity->generateSnippet(snippetCharLimit);
                if (!snippet.isEmpty())
                {
                    activityDescription.append("&nbsp;<hr>" + snippet);
                }
            }
        }
    }

    this->activityDescriptionLabel->setText(activityDescription);




    //////////////////////////////////////////////////////// Layout
    leftLayout = new QVBoxLayout();
    leftLayout->setAlignment(Qt::AlignTop);
    leftLayout->setContentsMargins(1, 0, 1, 0);
    leftLayout->setSpacing(1);
    leftLayout->addWidget(avatarButton, 0, Qt::AlignTop | Qt::AlignLeft);
    leftLayout->addStretch(0);



    // Original post available (inReplyTo) or object available (note, image...)
    if (!inReplyToMap.isEmpty()
        || (ASObject::canDisplayObject(activity->object()->getType())
            && activity->object()->getDeletedTime().isEmpty())
       )
    {
        this->openButton = new QPushButton("+", this);
        openButton->setSizePolicy(QSizePolicy::Ignored,
                                  QSizePolicy::Maximum);
        openButton->setToolTip("<b></b>"
                               + tr("Open referenced post"));
        connect(openButton, SIGNAL(clicked()),
                this, SLOT(openOriginalPost()));
        leftLayout->addWidget(openButton, 0, Qt::AlignHCenter);
    }


    rightLowerLayout = new QHBoxLayout();
    rightLowerLayout->setContentsMargins(0, 0, 0, 0);
    rightLowerLayout->addWidget(activityDescriptionLabel, 1);
    // This may also contain an AvatarButton for an "object person"

    rightLayout = new QVBoxLayout();
    rightLayout->setAlignment(Qt::AlignTop);
    rightLayout->addWidget(timestampLabel);
    rightLayout->addLayout(rightLowerLayout);

    // If the object is a person, such as someone following someone else, add an AvatarButton for them
    if (activity->object()->getType() == "person")
    {
        ASPerson *personObject = activity->personObject();
        if (personObject->getId() != authorId) // avoid cases like "JohnDoe updated JohnDoe"
        {
            this->haveTargetAvatar = true;
            this->targetAvatarButton = new AvatarButton(personObject,
                                                        this->pController,
                                                        this->globalObj,
                                                        globalObj->getMfAvatarSize(), // QSize(28,28)
                                                        this);
            rightLowerLayout->addWidget(targetAvatarButton, 0, Qt::AlignBottom);
        }
    }



    topLayout = new QHBoxLayout();
    topLayout->setContentsMargins(0, 0, 0, 0);
    if (authorId == pController->currentUserId())
    {
        topLayout->addLayout(rightLayout, 20);
        topLayout->addLayout(leftLayout,  1);
    }
    else // Normal item, not ours
    {
        topLayout->addLayout(leftLayout,  1);
        topLayout->addLayout(rightLayout, 20);
    }


    mainLayout = new QVBoxLayout();
    mainLayout->setContentsMargins(2, 1, 2, 4);
    mainLayout->addLayout(topLayout);

    if (highlightedByFilter) // Show which filtering rules caused this item to be highlighted
    {
        QVariantMap filterMatches = activity->getFilterMatches();
        if (filterMatches.value("filterAction").toInt() == FilterChecker::Highlight)
        {
            this->filterMatchesWidget = new FilterMatchesWidget(filterMatches, this);
            mainLayout->addWidget(filterMatchesWidget);
            // FIXME -- TODO: make this widget optional
        }
    }

    this->setLayout(mainLayout);

    qDebug() << "MinorFeedItem created";
}



MinorFeedItem::~MinorFeedItem()
{
    qDebug() << "MinorFeedItem destroyed";
}


/*
 * Pseudo-highlight for new items
 *
 */
void MinorFeedItem::setItemAsNew(bool isNew, bool informFeed)
{
    itemIsNew = isNew;

    if (itemIsNew)
    {
        this->setAutoFillBackground(true);
        this->setBackgroundRole(QPalette::Mid);

        timestampLabel->setStyleSheet("QLabel "
                                      "{ background-color: qlineargradient(spread:pad,"
                                      "                    x1:0, y1:0, x2:1, y2:0,"
                                      "                    stop:0   palette(highlight),"
                                      "                    stop:0.2 palette(base),"
                                      "                    stop:0.8 palette(base),"
                                      "                    stop:1   palette(highlight));"
                                      "}"
                                      "QLabel:hover "
                                      "{ color: palette(highlighted-text);   "
                                      "  background-color: palette(highlight)"
                                      "}");
    }
    else
    {
        this->setAutoFillBackground(false);
        this->setBackgroundRole(QPalette::Window);

        timestampLabel->setStyleSheet("QLabel:hover "
                                      "{ color: palette(highlighted-text);   "
                                      "  background-color: palette(highlight)"
                                      "}");

        if (informFeed)
        {
            bool wasHighlighted = false;
            if (this->itemHighlightType != -1)
            {
                wasHighlighted = true;
            }

            emit itemRead(wasHighlighted);
        }
    }
}

bool MinorFeedItem::isNew()
{
    return this->itemIsNew;
}


/*
 * Set/Update the fuzzy timestamp
 *
 * This will be called from time to time
 *
 */
void MinorFeedItem::setFuzzyTimeStamp()
{
    this->timestampLabel->setText(Timestamp::fuzzyTime(createdAtTimestamp));
}

void MinorFeedItem::syncAvatarFollowState()
{
    this->avatarButton->syncFollowState();
    if (haveTargetAvatar)
    {
        this->targetAvatarButton->syncFollowState();
    }
}


int MinorFeedItem::getItemHighlightType()
{
    return this->itemHighlightType;
}

QString MinorFeedItem::getActivityId()
{
    return this->activityId;
}



/****************************************************************************/
/******************************** SLOTS *************************************/
/****************************************************************************/



void MinorFeedItem::openOriginalPost()
{
    // Create a fake activity for the object
    QVariantMap originalPostMap;
    if (!inReplyToMap.isEmpty())
    {
        originalPostMap.insert("object", this->inReplyToMap);
        originalPostMap.insert("actor",
                               this->inReplyToMap.value("author").toMap());
    }
    else
    {
        originalPostMap.insert("object", this->originalObjectMap);
        originalPostMap.insert("actor",
                               this->originalObjectMap.value("author").toMap());
    }

    ASActivity *originalPostActivity = new ASActivity(originalPostMap,
                                                      pController->currentUserId(),
                                                      this);

    Post *referencedPost = new Post(originalPostActivity,
                                    false, // Not highlighted
                                    true,  // Post is standalone
                                    pController,
                                    globalObj,
                                    nullptr); // Make it an independent window
    referencedPost->show();
    connect(pController, SIGNAL(commentsReceived(QVariantList,QString)),
            referencedPost, SLOT(setAllComments(QVariantList,QString)));
    referencedPost->requestCommenterComments();
}



void MinorFeedItem::showUrlInfo(QString url)
{
    if (!url.isEmpty())
    {
        this->pController->showTransientMessage(url);

        qDebug() << "Link hovered in Minor Feed:" << url;
    }
    else
    {
        this->pController->showTransientMessage("");
    }
}


/****************************************************************************/
/******************************* PROTECTED **********************************/
/****************************************************************************/


/*
 * On mouse click in any part of the item, set it as read
 *
 */
void MinorFeedItem::mousePressEvent(QMouseEvent *event)
{
    if (itemIsNew)
    {
        this->setItemAsNew(false, // Mark as not new
                           true); // Inform the feed

        // Avoid flickering of the "new" effect later
        this->timestampLabel->repaint();
    }

    event->accept();
}


/*
 * Ensure URL info in statusbar is hidden when the mouse leaves the item
 *
 */
void MinorFeedItem::leaveEvent(QEvent *event)
{
    this->pController->showTransientMessage("");

    event->accept();
}