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
|
/*
* 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 "contactlist.h"
ContactList::ContactList(PumpController *pumpController,
GlobalObject *globalObject,
QString listType,
QWidget *parent) : QWidget(parent)
{
this->pController = pumpController;
this->globalObj = globalObject;
QString filterNote = tr("Type a partial name or ID to find a contact...")
+ " (Control+F)";
this->filterLineEdit = new QLineEdit(this);
filterLineEdit->setPlaceholderText(filterNote);
filterLineEdit->setToolTip("<b></b>" + filterNote); // HTMLized for wordwrapping
connect(filterLineEdit, SIGNAL(textChanged(QString)),
this, SLOT(filterList(QString)));
int iconSize = this->filterLineEdit->sizeHint().height(); // filterLineEdit->font().pixelSize()?
this->filterIcon = new QLabel(this);
filterIcon->setAlignment(Qt::AlignCenter);
filterIcon->setPixmap(QIcon::fromTheme("edit-find",
QIcon(":/images/menu-find.png"))
.pixmap(iconSize, iconSize));
this->matchesLabel = new QLabel(this);
matchesLabel->hide();
this->removeFilterButton = new QPushButton(QIcon::fromTheme("view-list-icons"),
tr("F&ull List"),
this);
removeFilterButton->setDisabled(true); // Disabled initially, until a search happens
connect(removeFilterButton, SIGNAL(clicked()),
filterLineEdit, SLOT(clear()));
goToFilterAction = new QAction(this);
goToFilterAction->setShortcut(QKeySequence("Ctrl+F"));
connect(goToFilterAction, SIGNAL(triggered()),
filterLineEdit, SLOT(setFocus()));
this->addAction(goToFilterAction);
// Layout
this->filterLayout = new QHBoxLayout();
filterLayout->addWidget(filterIcon, 0, Qt::AlignVCenter);
filterLayout->addWidget(filterLineEdit);
filterLayout->addWidget(matchesLabel);
filterLayout->addWidget(removeFilterButton);
this->contactsLayout = new QVBoxLayout();
this->contactsWidget = new QWidget(this);
contactsWidget->setLayout(contactsLayout);
this->contactsScrollArea = new QScrollArea(this);
contactsScrollArea->setWidget(contactsWidget);
contactsScrollArea->setWidgetResizable(true);
contactsScrollArea->setFrameStyle(QFrame::NoFrame);
contactsScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
this->mainLayout = new QVBoxLayout();
mainLayout->addWidget(contactsScrollArea);
mainLayout->addLayout(filterLayout);
this->setLayout(mainLayout);
// Add demo contacts
QVariantMap demoContactData;
QVariantMap demoContactHometown;
QVariantMap demoContactFollowed;
this->isFollowing = false;
if (listType == "following")
{
isFollowing = true;
demoContactData.insert("displayName", "Demo Contact");
demoContactData.insert("id", "democontact@pumpserver.org");
demoContactData.insert("url", "https://jancoding.wordpress.com");
demoContactFollowed.insert("followed", "true");
}
else
{
demoContactData.insert("displayName", "Demo Follower");
demoContactData.insert("id", "demofollower@pumpserver.org");
demoContactData.insert("url", "http://dianara.nongnu.org");
demoContactFollowed.insert("followed", "false");
}
demoContactHometown.insert("displayName", "Some city");
demoContactData.insert("location", demoContactHometown);
demoContactData.insert("pump_io", demoContactFollowed);
demoContactData.insert("published", "2013-05-01T00:00:00Z"); // Dianara's birthday
ASPerson *demoContactPerson = new ASPerson(demoContactData, this);
ContactCard *demoContactCard = new ContactCard(this->pController,
this->globalObj,
demoContactPerson, this);
this->contactsLayout->addWidget(demoContactCard);
this->contactsInList.append(demoContactCard);
qDebug() << "ContactList created";
}
ContactList::~ContactList()
{
qDebug() << "ContactList destroyed";
}
void ContactList::clearListContents()
{
foreach (ContactCard *card, contactsInList)
{
this->contactsLayout->removeWidget(card);
delete card;
}
this->contactsInList.clear();
this->contactsStringForExport.clear();
if (this->isFollowing)
{
this->globalObj->clearNickCompletionModel();
}
}
void ContactList::setListContents(QVariantList contactList)
{
qDebug() << "ContactList; Setting list contents";
QString contactInfoLineString;
QStringList followingIdStringList;
foreach (QVariant contact, contactList)
{
QVariantMap contactMap = contact.toMap();
if (!contactMap.keys().contains("id"))
{
/*
* Temporary hack to fix user profiles when the list comes
* from the site user list, which is currently empty,
* and contains ID only inside the profile property
*
*/
QString id = ASPerson::cleanupId(contactMap.value("profile").toMap()
.value("id").toString());
contactMap.insert("id", id);
QVariantMap followedMap;
followedMap.insert("followed", pController->userInFollowing(id));
contactMap.insert("pump_io", followedMap);
id.remove("@" + pController->currentServerUrl());
contactMap.insert("displayName", id);
contactMap.insert("url", pController->currentServerScheme()
+ pController->currentServerUrl()
+ "/" + id);
QVariantMap hrefMap;
hrefMap.insert("href", pController->currentServerScheme()
+ pController->currentServerUrl()
+ "/api/user/" + id + "/feed");
QVariantMap outboxMap;
outboxMap.insert("activity-outbox", hrefMap);
contactMap.insert("links", outboxMap);
}
ASPerson *person = new ASPerson(contactMap, this);
ContactCard *contactCard = new ContactCard(this->pController,
this->globalObj,
person,
this);
this->contactsLayout->addWidget(contactCard);
this->contactsInList.append(contactCard);
// Info for the string list used when exporting
contactInfoLineString = person->getName()
+ " <"
+ person->getId()
+ ">\n";
contactsStringForExport.append(contactInfoLineString);
if (this->isFollowing)
{
// Add to internal following list for PumpController
followingIdStringList.append(person->getId());
// Add also to GlobalObject's model for nick completion
this->globalObj->addToNickCompletionModel(person->getId(),
person->getNameWithFallback(),
person->getUrl());
}
} // end foreach
// Batch of contacts added to list, add them also to the internal list
if (this->isFollowing)
{
this->pController->updateInternalFollowingIdList(followingIdStringList);
this->globalObj->sortNickCompletionModel(); // FIXME: should only sort when the whole list is done
}
this->filterLineEdit->clear(); // Unfilter the list
}
QString ContactList::getContactsStringForExport()
{
return this->contactsStringForExport;
}
/*****************************************************************************/
/*********************************** SLOTS ***********************************/
/*****************************************************************************/
void ContactList::filterList(QString filterText)
{
qDebug() << "Filtering for contacts matching:" << filterText;
if (!filterText.isEmpty())
{
int matchCount = 0;
foreach (ContactCard *card, contactsInList)
{
if (card->getNameAndIdString().contains(filterText, Qt::CaseInsensitive))
{
card->show();
++matchCount;
}
else
{
card->hide();
}
}
matchesLabel->setText(QString("(%1)")
.arg(QLocale::system().toString(matchCount)));
matchesLabel->show();
removeFilterButton->setEnabled(true);
}
else // If no filter at all, more optimized version showing all
{
foreach (ContactCard *card, contactsInList)
{
card->show();
}
matchesLabel->setText("");
matchesLabel->hide();
removeFilterButton->setDisabled(true);
}
}
void ContactList::addSingleContact(ASPerson *contact)
{
ContactCard *card = new ContactCard(this->pController,
this->globalObj,
contact,
this);
this->contactsLayout->insertWidget(0, card);
this->contactsInList.append(card);
emit contactCountChanged(1);
// This check is actually unnecessary, since this slot is only called
if (this->isFollowing) // for the 'following' list
{
QStringList contactsToAdd;
contactsToAdd.append(contact->getId());
this->pController->updateInternalFollowingIdList(contactsToAdd);
// Add also to GlobalObject's model for nick completion
this->globalObj->addToNickCompletionModel(contact->getId(),
contact->getNameWithFallback(),
contact->getUrl());
this->globalObj->sortNickCompletionModel();
}
}
void ContactList::removeSingleContact(ASPerson *contact)
{
foreach (ContactCard *card, contactsInList)
{
/* Ignore disabled cards, to avoid substracting more than once, in case
* there were more ContactCards for the same contact, from following
* and unfollowing previously.
*
*/
if (card->isEnabled() &&
card->getId() == contact->getId())
{
emit contactCountChanged(-1);
this->pController->removeFromInternalFollowingList(contact->getId());
// Remove from GlobalObject's model for nick completion, too
this->globalObj->removeFromNickCompletionModel(contact->getId());
card->setDisabled(true);
}
}
contact->deleteLater();
}
|