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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
|
/**
* This file is part of TelepathyQt
*
* @copyright Copyright (C) 2008-2010 Collabora Ltd. <http://www.collabora.co.uk/>
* @copyright Copyright (C) 2008-2010 Nokia Corporation
* @license LGPL 2.1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <TelepathyQt/ContactManager>
#include "TelepathyQt/contact-manager-internal.h"
#include "TelepathyQt/_gen/contact-manager.moc.hpp"
#include "TelepathyQt/debug-internal.h"
#include "TelepathyQt/future-internal.h"
#include <TelepathyQt/AvatarData>
#include <TelepathyQt/Connection>
#include <TelepathyQt/ConnectionLowlevel>
#include <TelepathyQt/ContactFactory>
#include <TelepathyQt/PendingChannel>
#include <TelepathyQt/PendingContactAttributes>
#include <TelepathyQt/PendingContacts>
#include <TelepathyQt/PendingFailure>
#include <TelepathyQt/PendingHandles>
#include <TelepathyQt/PendingVariantMap>
#include <TelepathyQt/ReferencedHandles>
#include <TelepathyQt/Utils>
#include <QMap>
namespace Tp
{
struct TP_QT_NO_EXPORT ContactManager::Private
{
Private(ContactManager *parent, Connection *connection);
~Private();
// avatar specific methods
bool buildAvatarFileName(QString token, bool createDir,
QString &avatarFileName, QString &mimeTypeFileName);
Features realFeatures(const Features &features);
QSet<QString> interfacesForFeatures(const Features &features);
ContactManager *parent;
WeakPtr<Connection> connection;
ContactManager::Roster *roster;
QMap<uint, WeakPtr<Contact> > contacts;
QMap<Feature, bool> tracking;
Features supportedFeatures;
// avatar
QSet<ContactPtr> requestAvatarsQueue;
bool requestAvatarsIdle;
// contact info
PendingRefreshContactInfo *refreshInfoOp;
};
ContactManager::Private::Private(ContactManager *parent, Connection *connection)
: parent(parent),
connection(connection),
roster(new ContactManager::Roster(parent)),
requestAvatarsIdle(false),
refreshInfoOp(0)
{
}
ContactManager::Private::~Private()
{
delete refreshInfoOp;
delete roster;
}
bool ContactManager::Private::buildAvatarFileName(QString token, bool createDir,
QString &avatarFileName, QString &mimeTypeFileName)
{
QString cacheDir = QString(QLatin1String(qgetenv("XDG_CACHE_HOME")));
if (cacheDir.isEmpty()) {
cacheDir = QString(QLatin1String("%1/.cache")).arg(QLatin1String(qgetenv("HOME")));
}
ConnectionPtr conn(parent->connection());
QString path = QString(QLatin1String("%1/telepathy/avatars/%2/%3")).
arg(cacheDir).arg(conn->cmName()).arg(conn->protocolName());
if (createDir && !QDir().mkpath(path)) {
return false;
}
avatarFileName = QString(QLatin1String("%1/%2")).arg(path).arg(escapeAsIdentifier(token));
mimeTypeFileName = QString(QLatin1String("%1.mime")).arg(avatarFileName);
return true;
}
Features ContactManager::Private::realFeatures(const Features &features)
{
Features ret(features);
ret.unite(parent->connection()->contactFactory()->features());
// FeatureAvatarData depends on FeatureAvatarToken
if (ret.contains(Contact::FeatureAvatarData) &&
!ret.contains(Contact::FeatureAvatarToken)) {
ret.insert(Contact::FeatureAvatarToken);
}
return ret;
}
QSet<QString> ContactManager::Private::interfacesForFeatures(const Features &features)
{
Features supported = parent->supportedFeatures();
QSet<QString> ret;
foreach (const Feature &feature, features) {
parent->ensureTracking(feature);
if (supported.contains(feature)) {
// Only query interfaces which are reported as supported to not get an error
ret.insert(parent->featureToInterface(feature));
}
}
return ret;
}
ContactManager::PendingRefreshContactInfo::PendingRefreshContactInfo(const ConnectionPtr &conn)
: PendingOperation(conn),
mConn(conn)
{
}
ContactManager::PendingRefreshContactInfo::~PendingRefreshContactInfo()
{
}
void ContactManager::PendingRefreshContactInfo::addContact(Contact *contact)
{
mToRequest.insert(contact->handle()[0]);
}
void ContactManager::PendingRefreshContactInfo::refreshInfo()
{
Q_ASSERT(!mToRequest.isEmpty());
if (!mConn->isValid()) {
setFinishedWithError(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"));
return;
}
if (!mConn->hasInterface(TP_QT_IFACE_CONNECTION_INTERFACE_CONTACT_INFO)) {
setFinishedWithError(TP_QT_ERROR_NOT_IMPLEMENTED,
QLatin1String("Connection does not support ContactInfo interface"));
return;
}
debug() << "Calling ContactInfo.RefreshContactInfo for" << mToRequest.size() << "handles";
Client::ConnectionInterfaceContactInfoInterface *contactInfoInterface =
mConn->interface<Client::ConnectionInterfaceContactInfoInterface>();
Q_ASSERT(contactInfoInterface);
PendingVoid *nested = new PendingVoid(
contactInfoInterface->RefreshContactInfo(mToRequest.toList()),
mConn);
connect(nested,
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onRefreshInfoFinished(Tp::PendingOperation*)));
}
void ContactManager::PendingRefreshContactInfo::onRefreshInfoFinished(PendingOperation *op)
{
if (op->isError()) {
warning() << "ContactInfo.RefreshContactInfo failed with" <<
op->errorName() << "-" << op->errorMessage();
setFinishedWithError(op->errorName(), op->errorMessage());
} else {
debug() << "Got reply to ContactInfo.RefreshContactInfo";
setFinished();
}
}
/**
* \class ContactManager
* \ingroup clientconn
* \headerfile TelepathyQt/contact-manager.h <TelepathyQt/ContactManager>
*
* \brief The ContactManager class is responsible for managing contacts.
*
* See \ref async_model, \ref shared_ptr
*/
/**
* Construct a new ContactManager object.
*
* \param connection The connection owning this ContactManager.
*/
ContactManager::ContactManager(Connection *connection)
: Object(),
mPriv(new Private(this, connection))
{
}
/**
* Class destructor.
*/
ContactManager::~ContactManager()
{
delete mPriv;
}
/**
* Return the connection owning this ContactManager.
*
* \return A pointer to the Connection object.
*/
ConnectionPtr ContactManager::connection() const
{
return ConnectionPtr(mPriv->connection);
}
/**
* Return the features that are expected to work on contacts on this ContactManager connection.
*
* This method requires Connection::FeatureCore to be ready.
*
* \return The supported features as a set of Feature objects.
*/
Features ContactManager::supportedFeatures() const
{
if (mPriv->supportedFeatures.isEmpty() &&
connection()->interfaces().contains(TP_QT_IFACE_CONNECTION_INTERFACE_CONTACTS)) {
Features allFeatures = Features()
<< Contact::FeatureAlias
<< Contact::FeatureAvatarToken
<< Contact::FeatureAvatarData
<< Contact::FeatureSimplePresence
<< Contact::FeatureCapabilities
<< Contact::FeatureLocation
<< Contact::FeatureInfo
<< Contact::FeatureRosterGroups
<< Contact::FeatureAddresses;
QStringList interfaces = connection()->lowlevel()->contactAttributeInterfaces();
foreach (const Feature &feature, allFeatures) {
if (interfaces.contains(featureToInterface(feature))) {
mPriv->supportedFeatures.insert(feature);
}
}
debug() << mPriv->supportedFeatures.size() << "contact features supported using" << this;
}
return mPriv->supportedFeatures;
}
/**
* Return the progress made in retrieving the contact list.
*
* Change notification is via the stateChanged() signal.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return The contact list state as #ContactListState.
* \sa stateChanged()
*/
ContactListState ContactManager::state() const
{
return mPriv->roster->state();
}
/**
* Return a list of relevant contacts (a reasonable guess as to what should
* be displayed as "the contact list").
*
* This may include any or all of: contacts whose presence the user receives,
* contacts who are allowed to see the user's presence, contacts stored in
* some persistent contact list on the server, contacts who the user
* has blocked from communicating with them, or contacts who are relevant
* in some other way.
*
* User interfaces displaying a contact list will probably want to filter this
* list and display some suitable subset of it.
*
* On protocols where there is no concept of presence or a centrally-stored
* contact list (like IRC), this method may return an empty list.
*
* Change notification is via the allKnownContactsChanged() signal.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return A set of pointers to the Contact objects.
* \sa allKnownContactsChanged()
*/
Contacts ContactManager::allKnownContacts() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
warning() << "Calling allKnownContacts() before FeatureRoster is ready";
return Contacts();
}
return mPriv->roster->allKnownContacts();
}
/**
* Return a list of user-defined contact list groups' names.
*
* Change notification is via the groupAdded(), groupRemoved() and groupRenamed() signals.
*
* This method requires Connection::FeatureRosterGroups to be ready.
*
* \return The list of user-defined contact list groups names.
* \sa groupMembersChanged(), groupAdded(), groupRemoved(), groupRenamed()
*/
QStringList ContactManager::allKnownGroups() const
{
if (!connection()->isReady(Connection::FeatureRosterGroups)) {
return QStringList();
}
return mPriv->roster->allKnownGroups();
}
/**
* Attempt to add an user-defined contact list group named \a group.
*
* On some protocols (e.g. XMPP) empty groups are not represented on the server,
* so disconnecting from the server and reconnecting might cause empty groups to
* vanish.
*
* The returned pending operation will finish successfully if the group already
* exists.
*
* Change notification is via the groupAdded() signal.
*
* This method requires Connection::FeatureRosterGroups to be ready.
*
* \param group The group name.
* \return A PendingOperation which will emit PendingOperation::finished
* when an attempt has been made to add an user-defined contact list group.
* \sa allKnownGroups(), groupAdded(), addContactsToGroup()
*/
PendingOperation *ContactManager::addGroup(const QString &group)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRosterGroups)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRosterGroups is not ready"),
connection());
}
return mPriv->roster->addGroup(group);
}
/**
* Attempt to remove an user-defined contact list group named \a group.
*
* Change notification is via the groupRemoved() signal.
*
* This method requires Connection::FeatureRosterGroups to be ready.
*
* \param group The group name.
* \return A PendingOperation which will emit PendingOperation::finished()
* when an attempt has been made to remove an user-defined contact list group.
* \sa allKnownGroups(), groupRemoved(), removeContactsFromGroup()
*/
PendingOperation *ContactManager::removeGroup(const QString &group)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRosterGroups)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRosterGroups is not ready"),
connection());
}
return mPriv->roster->removeGroup(group);
}
/**
* Return the contacts in the given user-defined contact list group
* named \a group.
*
* Change notification is via the groupMembersChanged() signal.
*
* This method requires Connection::FeatureRosterGroups to be ready.
*
* \param group The group name.
* \return A set of pointers to the Contact objects, or an empty set if the group does not exist.
* \sa allKnownGroups(), groupMembersChanged()
*/
Contacts ContactManager::groupContacts(const QString &group) const
{
if (!connection()->isReady(Connection::FeatureRosterGroups)) {
return Contacts();
}
return mPriv->roster->groupContacts(group);
}
/**
* Attempt to add the given \a contacts to the user-defined contact list
* group named \a group.
*
* Change notification is via the groupMembersChanged() signal.
*
* This method requires Connection::FeatureRosterGroups to be ready.
*
* \param group The group name.
* \param contacts Contacts to add.
* \return A PendingOperation which will emit PendingOperation::finished()
* when an attempt has been made to add the contacts to the user-defined
* contact list group.
* \sa groupMembersChanged(), groupContacts()
*/
PendingOperation *ContactManager::addContactsToGroup(const QString &group,
const QList<ContactPtr> &contacts)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRosterGroups)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRosterGroups is not ready"),
connection());
}
return mPriv->roster->addContactsToGroup(group, contacts);
}
/**
* Attempt to remove the given \a contacts from the user-defined contact list
* group named \a group.
*
* Change notification is via the groupMembersChanged() signal.
*
* This method requires Connection::FeatureRosterGroups to be ready.
*
* \param group The group name.
* \param contacts Contacts to remove.
* \return A PendingOperation which will PendingOperation::finished
* when an attempt has been made to remove the contacts from the user-defined
* contact list group.
* \sa groupMembersChanged(), groupContacts()
*/
PendingOperation *ContactManager::removeContactsFromGroup(const QString &group,
const QList<ContactPtr> &contacts)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRosterGroups)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRosterGroups is not ready"),
connection());
}
return mPriv->roster->removeContactsFromGroup(group, contacts);
}
/**
* Return whether subscribing to additional contacts' presence is supported.
*
* In some protocols, the list of contacts whose presence can be seen is
* fixed, so we can't subscribe to the presence of additional contacts.
*
* Notably, in link-local XMPP, you can see the presence of everyone on the
* local network, and trying to add more subscriptions would be meaningless.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if Contact::requestPresenceSubscription() and
* requestPresenceSubscription() are likely to succeed, \c false otherwise.
* \sa requestPresenceSubscription(), subscriptionRequestHasMessage()
*/
bool ContactManager::canRequestPresenceSubscription() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->canRequestPresenceSubscription();
}
/**
* Return whether a message can be sent when subscribing to contacts'
* presence.
*
* If no message will actually be sent, user interfaces should avoid prompting
* the user for a message, and use an empty string for the message argument.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if the message argument to Contact::requestPresenceSubscription() and
* requestPresenceSubscription() is actually used, \c false otherwise.
* \sa canRemovePresenceSubscription(), requestPresenceSubscription()
*/
bool ContactManager::subscriptionRequestHasMessage() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->subscriptionRequestHasMessage();
}
/**
* Attempt to subscribe to the presence of the given contacts.
*
* This operation is sometimes called "adding contacts to the buddy
* list" or "requesting authorization".
*
* On most protocols, the contacts will need to give permission
* before the user will be able to receive their presence: if so, they will
* be in presence state Contact::PresenceStateAsk until they authorize
* or deny the request.
*
* The returned PendingOperation will return successfully when a request to
* subscribe to the contacts' presence has been submitted, or fail if this
* cannot happen. In particular, it does not wait for the contacts to give
* permission for the presence subscription.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts whose presence is desired
* \param message A message from the user which is either transmitted to the
* contacts, or ignored, depending on the protocol
* \return A PendingOperation which will PendingOperation::finished()
* when an attempt has been made to subscribe to the contacts' presence.
* \sa canRequestPresenceSubscription(), subscriptionRequestHasMessage()
*/
PendingOperation *ContactManager::requestPresenceSubscription(
const QList<ContactPtr> &contacts, const QString &message)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRoster)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRoster is not ready"),
connection());
}
return mPriv->roster->requestPresenceSubscription(contacts, message);
}
/**
* Return whether the user can stop receiving the presence of a contact
* whose presence they have subscribed to.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if Contact::removePresenceSubscription() and
* removePresenceSubscription() are likely to succeed
* for contacts with subscription state Contact::PresenceStateYes,
* \c false otherwise.
* \sa removePresenceSubscription(), subscriptionRemovalHasMessage()
*/
bool ContactManager::canRemovePresenceSubscription() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->canRemovePresenceSubscription();
}
/**
* Return whether a message can be sent when removing an existing subscription
* to the presence of a contact.
*
* If no message will actually be sent, user interfaces should avoid prompting
* the user for a message, and use an empty string for the message argument.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if the message argument to Contact::removePresenceSubscription() and
* removePresenceSubscription() is actually used,
* for contacts with subscription state Contact::PresenceStateYes,
* \c false otherwise.
* \sa canRemovePresencePublication(), removePresenceSubscription()
*/
bool ContactManager::subscriptionRemovalHasMessage() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->subscriptionRemovalHasMessage();
}
/**
* Return whether the user can cancel a request to subscribe to a contact's
* presence before that contact has responded.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if Contact::removePresenceSubscription() and
* removePresenceSubscription() are likely to succeed
* for contacts with subscription state Contact::PresenceStateAsk,
* \c false otherwise.
* \sa removePresenceSubscription(), subscriptionRescindingHasMessage()
*/
bool ContactManager::canRescindPresenceSubscriptionRequest() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->canRescindPresenceSubscriptionRequest();
}
/**
* Return whether a message can be sent when cancelling a request to
* subscribe to the presence of a contact.
*
* If no message will actually be sent, user interfaces should avoid prompting
* the user for a message, and use an empty string for the message argument.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if the message argument to Contact::removePresenceSubscription() and
* removePresenceSubscription() is actually used,
* for contacts with subscription state Contact::PresenceStateAsk,
* \c false otherwise.
* \sa canRescindPresenceSubscriptionRequest(), removePresenceSubscription()
*/
bool ContactManager::subscriptionRescindingHasMessage() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->subscriptionRescindingHasMessage();
}
/**
* Attempt to stop receiving the presence of the given contacts, or cancel
* a request to subscribe to their presence that was previously sent.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts whose presence is no longer required
* \message A message from the user which is either transmitted to the
* contacts, or ignored, depending on the protocol
* \return A PendingOperation which will PendingOperation::finished()
* when an attempt has been made to remove any subscription to the contacts' presence.
* \sa canRemovePresenceSubscription(), canRescindPresenceSubscriptionRequest(),
* subscriptionRemovalHasMessage(), subscriptionRescindingHasMessage()
*/
PendingOperation *ContactManager::removePresenceSubscription(
const QList<ContactPtr> &contacts, const QString &message)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRoster)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRoster is not ready"),
connection());
}
return mPriv->roster->removePresenceSubscription(contacts, message);
}
/**
* Return true if the publication of the user's presence to contacts can be
* authorized.
*
* This is always true, unless the protocol has no concept of authorizing
* publication (in which case contacts' publication status can never be
* Contact::PresenceStateAsk).
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if Contact::authorizePresencePublication() and
* authorizePresencePublication() are likely to succeed
* for contacts with subscription state Contact::PresenceStateAsk,
* \c false otherwise.
* \sa publicationAuthorizationHasMessage(), authorizePresencePublication()
*/
bool ContactManager::canAuthorizePresencePublication() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->canAuthorizePresencePublication();
}
/**
* Return whether a message can be sent when authorizing a request from a
* contact that the user's presence is published to them.
*
* If no message will actually be sent, user interfaces should avoid prompting
* the user for a message, and use an empty string for the message argument.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if the message argument to Contact::authorizePresencePublication() and
* authorizePresencePublication() is actually used,
* for contacts with subscription state Contact::PresenceStateAsk,
* \c false otherwise.
* \sa canAuthorizePresencePublication(), authorizePresencePublication()
*/
bool ContactManager::publicationAuthorizationHasMessage() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->publicationAuthorizationHasMessage();
}
/**
* If the given contacts have asked the user to publish presence to them,
* grant permission for this publication to take place.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts who should be allowed to receive the user's
* presence
* \message A message from the user which is either transmitted to the
* contacts, or ignored, depending on the protocol
* \return A PendingOperation which will emit PendingOperation::fininshed
* when an attempt has been made to authorize publication of the user's presence
* to the contacts.
* \sa canAuthorizePresencePublication(), publicationAuthorizationHasMessage()
*/
PendingOperation *ContactManager::authorizePresencePublication(
const QList<ContactPtr> &contacts, const QString &message)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRoster)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRoster is not ready"),
connection());
}
return mPriv->roster->authorizePresencePublication(contacts, message);
}
/**
* Return whether a message can be sent when rejecting a request from a
* contact that the user's presence is published to them.
*
* If no message will actually be sent, user interfaces should avoid prompting
* the user for a message, and use an empty string for the message argument.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if the message argument to Contact::removePresencePublication() and
* removePresencePublication() is actually used,
* for contacts with subscription state Contact::PresenceStateAsk,
* \c false otherwise.
* \sa canRemovePresencePublication(), removePresencePublication()
*/
bool ContactManager::publicationRejectionHasMessage() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->publicationRejectionHasMessage();
}
/**
* Return true if the publication of the user's presence to contacts can be
* removed, even after permission has been given.
*
* (Rejecting requests for presence to be published is always allowed.)
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if Contact::removePresencePublication() and
* removePresencePublication() are likely to succeed
* for contacts with subscription state Contact::PresenceStateYes,
* \c false otherwise.
* \sa publicationRemovalHasMessage(), removePresencePublication()
*/
bool ContactManager::canRemovePresencePublication() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->canRemovePresencePublication();
}
/**
* Return whether a message can be sent when revoking earlier permission
* that the user's presence is published to a contact.
*
* If no message will actually be sent, user interfaces should avoid prompting
* the user for a message, and use an empty string for the message argument.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if the message argument to Contact::removePresencePublication and
* removePresencePublication() is actually used,
* for contacts with subscription state Contact::PresenceStateYes,
* \c false otherwise.
* \sa canRemovePresencePublication(), removePresencePublication()
*/
bool ContactManager::publicationRemovalHasMessage() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->publicationRemovalHasMessage();
}
/**
* If the given contacts have asked the user to publish presence to them,
* deny this request (this should always succeed, unless a network error
* occurs).
*
* If the given contacts already have permission to receive
* the user's presence, attempt to revoke that permission (this might not
* be supported by the protocol - canRemovePresencePublication
* indicates whether it is likely to succeed).
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts who should no longer be allowed to receive the
* user's presence
* \message A message from the user which is either transmitted to the
* contacts, or ignored, depending on the protocol
* \return A PendingOperation which will emit PendingOperation::finished()
* when an attempt has been made to remove any publication of the user's presence to the
* contacts.
* \sa canRemovePresencePublication(), publicationRejectionHasMessage(),
* publicationRemovalHasMessage()
*/
PendingOperation *ContactManager::removePresencePublication(
const QList<ContactPtr> &contacts, const QString &message)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRoster)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRoster is not ready"),
connection());
}
return mPriv->roster->removePresencePublication(contacts, message);
}
/**
* Remove completely contacts from the server. It has the same effect than
* calling removePresencePublication() and removePresenceSubscription(),
* but also remove from 'stored' list if it exists.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts who should be removed
* \message A message from the user which is either transmitted to the
* contacts, or ignored, depending on the protocol
* \return A PendingOperation which will emit PendingOperation::finished
* when an attempt has been made to remove any publication of the user's presence to
* the contacts.
*/
PendingOperation *ContactManager::removeContacts(
const QList<ContactPtr> &contacts, const QString &message)
{
if (!connection()->isValid()) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"),
connection());
} else if (!connection()->isReady(Connection::FeatureRoster)) {
return new PendingFailure(TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureRoster is not ready"),
connection());
}
return mPriv->roster->removeContacts(contacts, message);
}
/**
* Return whether this protocol has a list of blocked contacts.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if blockContacts() is likely to succeed, \c false otherwise.
*/
bool ContactManager::canBlockContacts() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->canBlockContacts();
}
/**
* Return whether this protocol can report abusive contacts.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \return \c true if reporting abuse when blocking contacts is supported, \c false otherwise.
*/
bool ContactManager::canReportAbuse() const
{
if (!connection()->isReady(Connection::FeatureRoster)) {
return false;
}
return mPriv->roster->canReportAbuse();
}
/**
* Block the given contacts. Blocked contacts cannot send messages
* to the user; depending on the protocol, blocking a contact may
* have other effects.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts that should be blocked.
* \return A PendingOperation which will emit PendingOperation::finished()
* when an attempt has been made to take the requested action.
* \sa canBlockContacts(), unblockContacts(), blockContactsAndReportAbuse()
*/
PendingOperation *ContactManager::blockContacts(const QList<ContactPtr> &contacts)
{
return mPriv->roster->blockContacts(contacts, true, false);
}
/**
* Block the given contacts and additionally report abusive behaviour
* to the server.
*
* If reporting abusive behaviour is not supported by the protocol,
* this method has the same effect as blockContacts().
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts who should be added to the list of blocked contacts.
* \return A PendingOperation which will emit PendingOperation::finished()
* when an attempt has been made to take the requested action.
* \sa canBlockContacts(), canReportAbuse(), blockContacts()
*/
PendingOperation *ContactManager::blockContactsAndReportAbuse(
const QList<ContactPtr> &contacts)
{
return mPriv->roster->blockContacts(contacts, true, true);
}
/**
* Unblock the given contacts.
*
* This method requires Connection::FeatureRoster to be ready.
*
* \param contacts Contacts that should be unblocked.
* \return A PendingOperation which will emit PendingOperation::finished()
* when an attempt has been made to take the requested action.
* \sa canBlockContacts(), blockContacts(), blockContactsAndReportAbuse()
*/
PendingOperation *ContactManager::unblockContacts(const QList<ContactPtr> &contacts)
{
return mPriv->roster->blockContacts(contacts, false, false);
}
PendingContacts *ContactManager::contactsForHandles(const UIntList &handles,
const Features &features)
{
QMap<uint, ContactPtr> satisfyingContacts;
QSet<uint> otherContacts;
Features missingFeatures;
if (!connection()->isValid()) {
return new PendingContacts(ContactManagerPtr(this), handles, features, Features(),
QStringList(), satisfyingContacts, otherContacts,
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"));
} else if (!connection()->isReady(Connection::FeatureCore)) {
return new PendingContacts(ContactManagerPtr(this), handles, features, Features(),
QStringList(), satisfyingContacts, otherContacts,
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureCore is not ready"));
}
Features realFeatures = mPriv->realFeatures(features);
ConnectionLowlevelPtr connLowlevel = connection()->lowlevel();
if (connLowlevel->hasImmortalHandles() && realFeatures.isEmpty()) {
// try to avoid a roundtrip if all handles have an id set and no feature was requested
foreach (uint handle, handles) {
if (connLowlevel->hasContactId(handle)) {
ContactPtr contact = ensureContact(handle,
connLowlevel->contactId(handle), realFeatures);
satisfyingContacts.insert(handle, contact);
}
}
}
foreach (uint handle, handles) {
ContactPtr contact = lookupContactByHandle(handle);
if (contact) {
if ((realFeatures - contact->requestedFeatures()).isEmpty()) {
// Contact exists and has all the requested features
satisfyingContacts.insert(handle, contact);
} else {
// Contact exists but is missing features
otherContacts.insert(handle);
missingFeatures.unite(realFeatures - contact->requestedFeatures());
}
} else {
// Contact doesn't exist - we need to get all of the features (same as unite(features))
missingFeatures = realFeatures;
otherContacts.insert(handle);
}
}
QSet<QString> interfaces = mPriv->interfacesForFeatures(missingFeatures);
PendingContacts *contacts =
new PendingContacts(ContactManagerPtr(this), handles, features, missingFeatures,
interfaces.toList(), satisfyingContacts, otherContacts);
return contacts;
}
PendingContacts *ContactManager::contactsForHandles(const ReferencedHandles &handles,
const Features &features)
{
return contactsForHandles(handles.toList(), features);
}
PendingContacts *ContactManager::contactsForHandles(const HandleIdentifierMap &handles,
const Features &features)
{
connection()->lowlevel()->injectContactIds(handles);
return contactsForHandles(handles.keys(), features);
}
PendingContacts *ContactManager::contactsForIdentifiers(const QStringList &identifiers,
const Features &features)
{
if (!connection()->isValid()) {
return new PendingContacts(ContactManagerPtr(this), identifiers,
PendingContacts::ForIdentifiers, features, QStringList(),
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"));
} else if (!connection()->isReady(Connection::FeatureCore)) {
return new PendingContacts(ContactManagerPtr(this), identifiers,
PendingContacts::ForIdentifiers, features, QStringList(),
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureCore is not ready"));
}
Features realFeatures = mPriv->realFeatures(features);
PendingContacts *contacts = new PendingContacts(ContactManagerPtr(this), identifiers,
PendingContacts::ForIdentifiers, realFeatures, QStringList());
return contacts;
}
/**
* Request contacts and enable their \a features using a given field in their vCards.
*
* This method requires Connection::FeatureCore to be ready.
*
* \param vcardField The vCard field of the addresses we are requesting.
* Supported fields can be found in ProtocolInfo::addressableVCardFields().
* \param vcardAddresses The addresses to get contacts for. The address types must match
* the given vCard field.
* \param features The Contact features to enable.
* \return A PendingContacts, which will emit PendingContacts::finished
* when the contacts are retrieved or an error occurred.
* \sa contactsForHandles(), contactsForIdentifiers(), contactsForUris(),
* ProtocolInfo::normalizeVCardAddress()
*/
PendingContacts *ContactManager::contactsForVCardAddresses(const QString &vcardField,
const QStringList &vcardAddresses, const Features &features)
{
if (!connection()->isValid()) {
return new PendingContacts(ContactManagerPtr(this), vcardField, vcardAddresses,
features, QStringList(),
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"));
} else if (!connection()->isReady(Connection::FeatureCore)) {
return new PendingContacts(ContactManagerPtr(this), vcardField, vcardAddresses,
features, QStringList(),
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureCore is not ready"));
}
Features realFeatures = mPriv->realFeatures(features);
QSet<QString> interfaces = mPriv->interfacesForFeatures(realFeatures);
PendingContacts *contacts = new PendingContacts(ContactManagerPtr(this), vcardField,
vcardAddresses, realFeatures, interfaces.toList());
return contacts;
}
/**
* Request contacts and enable their \a features using the given URI addresses.
*
* This method requires Connection::FeatureCore to be ready.
*
* \param uris The URI addresses to get contacts for.
* Supported schemes can be found in ProtocolInfo::addressableUriSchemes().
* \param features The Contact features to enable.
* \return A PendingContacts, which will emit PendingContacts::finished
* when the contacts are retrieved or an error occurred.
* \sa contactsForHandles(), contactsForIdentifiers(), contactsForVCardAddresses(),
* ProtocolInfo::normalizeContactUri()
*/
PendingContacts *ContactManager::contactsForUris(const QStringList &uris,
const Features &features)
{
if (!connection()->isValid()) {
return new PendingContacts(ContactManagerPtr(this), uris,
PendingContacts::ForUris, features, QStringList(),
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"));
} else if (!connection()->isReady(Connection::FeatureCore)) {
return new PendingContacts(ContactManagerPtr(this), uris,
PendingContacts::ForUris, features, QStringList(),
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureCore is not ready"));
}
Features realFeatures = mPriv->realFeatures(features);
QSet<QString> interfaces = mPriv->interfacesForFeatures(realFeatures);
PendingContacts *contacts = new PendingContacts(ContactManagerPtr(this), uris,
PendingContacts::ForUris, realFeatures, interfaces.toList());
return contacts;
}
PendingContacts *ContactManager::upgradeContacts(const QList<ContactPtr> &contacts,
const Features &features)
{
if (!connection()->isValid()) {
return new PendingContacts(ContactManagerPtr(this), contacts, features,
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection is invalid"));
} else if (!connection()->isReady(Connection::FeatureCore)) {
return new PendingContacts(ContactManagerPtr(this), contacts, features,
TP_QT_ERROR_NOT_AVAILABLE,
QLatin1String("Connection::FeatureCore is not ready"));
}
return new PendingContacts(ContactManagerPtr(this), contacts, features);
}
ContactPtr ContactManager::lookupContactByHandle(uint handle)
{
ContactPtr contact;
if (mPriv->contacts.contains(handle)) {
contact = ContactPtr(mPriv->contacts.value(handle));
if (!contact) {
// Dangling weak pointer, remove it
mPriv->contacts.remove(handle);
}
}
return contact;
}
/**
* Start a request to retrieve the avatar for the given \a contacts.
*
* Force the request of the avatar data. This method returns directly, emitting
* Contact::avatarTokenChanged() and Contact::avatarDataChanged() signals once the token
* and data are fetched from the server.
*
* This is only useful if the avatar token is unknown; see Contact::isAvatarTokenKnown().
* It happens in the case of offline XMPP contacts, because the server does not
* send the token for them and an explicit request of the avatar data is needed.
*
* This method requires Contact::FeatureAvatarData to be ready.
*
* \sa Contact::avatarData(), Contact::avatarDataChanged(),
* Contact::avatarToken(), Contact::avatarTokenChanged()
*/
void ContactManager::requestContactAvatars(const QList<ContactPtr> &contacts)
{
if (contacts.isEmpty()) {
return;
}
if (!mPriv->requestAvatarsIdle) {
mPriv->requestAvatarsIdle = true;
QTimer::singleShot(0, this, SLOT(doRequestAvatars()));
}
mPriv->requestAvatarsQueue.unite(contacts.toSet());
}
/**
* Refresh information for the given contact.
*
* Once the information is retrieved infoFieldsChanged() will be emitted.
*
* This method requires Contact::FeatureInfo to be ready.
*
* \return A PendingOperation, which will emit PendingOperation::finished
* when the call has finished.
* \sa infoFieldsChanged()
*/
PendingOperation *ContactManager::refreshContactInfo(const QList<ContactPtr> &contacts)
{
if (!mPriv->refreshInfoOp) {
mPriv->refreshInfoOp = new PendingRefreshContactInfo(connection());
QTimer::singleShot(0, this, SLOT(doRefreshInfo()));
}
foreach (const ContactPtr &contact, contacts) {
mPriv->refreshInfoOp->addContact(contact.data());
}
return mPriv->refreshInfoOp;
}
void ContactManager::onAliasesChanged(const AliasPairList &aliases)
{
debug() << "Got AliasesChanged for" << aliases.size() << "contacts";
foreach (AliasPair pair, aliases) {
ContactPtr contact = lookupContactByHandle(pair.handle);
if (contact) {
contact->receiveAlias(pair.alias);
}
}
}
void ContactManager::doRequestAvatars()
{
Q_ASSERT(mPriv->requestAvatarsIdle);
QSet<ContactPtr> contacts = mPriv->requestAvatarsQueue;
Q_ASSERT(contacts.size() > 0);
mPriv->requestAvatarsQueue.clear();
mPriv->requestAvatarsIdle = false;
int found = 0;
UIntList notFound;
foreach (const ContactPtr &contact, contacts) {
if (!contact) {
continue;
}
QString avatarFileName;
QString mimeTypeFileName;
bool success = (contact->isAvatarTokenKnown() &&
mPriv->buildAvatarFileName(contact->avatarToken(), false,
avatarFileName, mimeTypeFileName));
/* Check if the avatar is already in the cache */
if (success && QFile::exists(avatarFileName)) {
QFile mimeTypeFile(mimeTypeFileName);
mimeTypeFile.open(QIODevice::ReadOnly);
QString mimeType = QString(QLatin1String(mimeTypeFile.readAll()));
mimeTypeFile.close();
found++;
contact->receiveAvatarData(AvatarData(avatarFileName, mimeType));
continue;
}
notFound << contact->handle()[0];
}
if (found > 0) {
debug() << "Avatar(s) found in cache for" << found << "contact(s)";
}
if (found == contacts.size()) {
return;
}
debug() << "Requesting avatar(s) for" << contacts.size() - found << "contact(s)";
Client::ConnectionInterfaceAvatarsInterface *avatarsInterface =
connection()->interface<Client::ConnectionInterfaceAvatarsInterface>();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(
avatarsInterface->RequestAvatars(notFound),
this);
connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), watcher,
SLOT(deleteLater()));
}
void ContactManager::onAvatarUpdated(uint handle, const QString &token)
{
debug() << "Got AvatarUpdate for contact with handle" << handle;
ContactPtr contact = lookupContactByHandle(handle);
if (contact) {
contact->receiveAvatarToken(token);
}
}
void ContactManager::onAvatarRetrieved(uint handle, const QString &token,
const QByteArray &data, const QString &mimeType)
{
QString avatarFileName;
QString mimeTypeFileName;
debug() << "Got AvatarRetrieved for contact with handle" << handle;
bool success = mPriv->buildAvatarFileName(token, true, avatarFileName,
mimeTypeFileName);
if (success) {
debug() << "Write avatar in cache for handle" << handle;
debug() << "Filename:" << avatarFileName;
debug() << "MimeType:" << mimeType;
QTemporaryFile mimeTypeFile(mimeTypeFileName);
mimeTypeFile.open();
mimeTypeFile.write(mimeType.toLatin1());
mimeTypeFile.setAutoRemove(false);
mimeTypeFile.rename(mimeTypeFileName);
QTemporaryFile avatarFile(avatarFileName);
avatarFile.open();
avatarFile.write(data);
avatarFile.setAutoRemove(false);
avatarFile.rename(avatarFileName);
}
ContactPtr contact = lookupContactByHandle(handle);
if (contact) {
contact->setAvatarToken(token);
contact->receiveAvatarData(AvatarData(avatarFileName, mimeType));
}
}
void ContactManager::onPresencesChanged(const SimpleContactPresences &presences)
{
debug() << "Got PresencesChanged for" << presences.size() << "contacts";
foreach (uint handle, presences.keys()) {
ContactPtr contact = lookupContactByHandle(handle);
if (contact) {
contact->receiveSimplePresence(presences[handle]);
}
}
}
void ContactManager::onCapabilitiesChanged(const ContactCapabilitiesMap &caps)
{
debug() << "Got ContactCapabilitiesChanged for" << caps.size() << "contacts";
foreach (uint handle, caps.keys()) {
ContactPtr contact = lookupContactByHandle(handle);
if (contact) {
contact->receiveCapabilities(caps[handle]);
}
}
}
void ContactManager::onLocationUpdated(uint handle, const QVariantMap &location)
{
debug() << "Got LocationUpdated for contact with handle" << handle;
ContactPtr contact = lookupContactByHandle(handle);
if (contact) {
contact->receiveLocation(location);
}
}
void ContactManager::onContactInfoChanged(uint handle, const Tp::ContactInfoFieldList &info)
{
debug() << "Got ContactInfoChanged for contact with handle" << handle;
ContactPtr contact = lookupContactByHandle(handle);
if (contact) {
contact->receiveInfo(info);
}
}
void ContactManager::doRefreshInfo()
{
PendingRefreshContactInfo *op = mPriv->refreshInfoOp;
Q_ASSERT(op);
mPriv->refreshInfoOp = 0;
op->refreshInfo();
}
ContactPtr ContactManager::ensureContact(const ReferencedHandles &handle,
const Features &features, const QVariantMap &attributes)
{
uint bareHandle = handle[0];
ContactPtr contact = lookupContactByHandle(bareHandle);
if (!contact) {
contact = connection()->contactFactory()->construct(this, handle, features, attributes);
mPriv->contacts.insert(bareHandle, contact);
}
contact->augment(features, attributes);
return contact;
}
ContactPtr ContactManager::ensureContact(uint bareHandle, const QString &id,
const Features &features)
{
ContactPtr contact = lookupContactByHandle(bareHandle);
if (!contact) {
QVariantMap attributes;
attributes.insert(TP_QT_IFACE_CONNECTION + QLatin1String("/contact-id"), id);
contact = connection()->contactFactory()->construct(this,
ReferencedHandles(connection(), HandleTypeContact, UIntList() << bareHandle),
features, attributes);
mPriv->contacts.insert(bareHandle, contact);
// do not call augment here as this is a fake contact
}
return contact;
}
QString ContactManager::featureToInterface(const Feature &feature)
{
if (feature == Contact::FeatureAlias) {
return TP_QT_IFACE_CONNECTION_INTERFACE_ALIASING;
} else if (feature == Contact::FeatureAvatarToken) {
return TP_QT_IFACE_CONNECTION_INTERFACE_AVATARS;
} else if (feature == Contact::FeatureAvatarData) {
return TP_QT_IFACE_CONNECTION_INTERFACE_AVATARS;
} else if (feature == Contact::FeatureSimplePresence) {
return TP_QT_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE;
} else if (feature == Contact::FeatureCapabilities) {
return TP_QT_IFACE_CONNECTION_INTERFACE_CONTACT_CAPABILITIES;
} else if (feature == Contact::FeatureLocation) {
return TP_QT_IFACE_CONNECTION_INTERFACE_LOCATION;
} else if (feature == Contact::FeatureInfo) {
return TP_QT_IFACE_CONNECTION_INTERFACE_CONTACT_INFO;
} else if (feature == Contact::FeatureRosterGroups) {
return TP_QT_IFACE_CONNECTION_INTERFACE_CONTACT_GROUPS;
} else if (feature == Contact::FeatureAddresses) {
return TP_QT_IFACE_CONNECTION_INTERFACE_ADDRESSING;
} else {
warning() << "ContactManager doesn't know which interface corresponds to feature"
<< feature;
return QString();
}
}
void ContactManager::ensureTracking(const Feature &feature)
{
if (mPriv->tracking[feature]) {
return;
}
ConnectionPtr conn(connection());
if (feature == Contact::FeatureAlias) {
Client::ConnectionInterfaceAliasingInterface *aliasingInterface =
conn->interface<Client::ConnectionInterfaceAliasingInterface>();
connect(aliasingInterface,
SIGNAL(AliasesChanged(Tp::AliasPairList)),
SLOT(onAliasesChanged(Tp::AliasPairList)));
} else if (feature == Contact::FeatureAvatarData) {
Client::ConnectionInterfaceAvatarsInterface *avatarsInterface =
conn->interface<Client::ConnectionInterfaceAvatarsInterface>();
connect(avatarsInterface,
SIGNAL(AvatarRetrieved(uint,QString,QByteArray,QString)),
SLOT(onAvatarRetrieved(uint,QString,QByteArray,QString)));
} else if (feature == Contact::FeatureAvatarToken) {
Client::ConnectionInterfaceAvatarsInterface *avatarsInterface =
conn->interface<Client::ConnectionInterfaceAvatarsInterface>();
connect(avatarsInterface,
SIGNAL(AvatarUpdated(uint,QString)),
SLOT(onAvatarUpdated(uint,QString)));
} else if (feature == Contact::FeatureCapabilities) {
Client::ConnectionInterfaceContactCapabilitiesInterface *contactCapabilitiesInterface =
conn->interface<Client::ConnectionInterfaceContactCapabilitiesInterface>();
connect(contactCapabilitiesInterface,
SIGNAL(ContactCapabilitiesChanged(Tp::ContactCapabilitiesMap)),
SLOT(onCapabilitiesChanged(Tp::ContactCapabilitiesMap)));
} else if (feature == Contact::FeatureInfo) {
Client::ConnectionInterfaceContactInfoInterface *contactInfoInterface =
conn->interface<Client::ConnectionInterfaceContactInfoInterface>();
connect(contactInfoInterface,
SIGNAL(ContactInfoChanged(uint,Tp::ContactInfoFieldList)),
SLOT(onContactInfoChanged(uint,Tp::ContactInfoFieldList)));
} else if (feature == Contact::FeatureLocation) {
Client::ConnectionInterfaceLocationInterface *locationInterface =
conn->interface<Client::ConnectionInterfaceLocationInterface>();
connect(locationInterface,
SIGNAL(LocationUpdated(uint,QVariantMap)),
SLOT(onLocationUpdated(uint,QVariantMap)));
} else if (feature == Contact::FeatureSimplePresence) {
Client::ConnectionInterfaceSimplePresenceInterface *simplePresenceInterface =
conn->interface<Client::ConnectionInterfaceSimplePresenceInterface>();
connect(simplePresenceInterface,
SIGNAL(PresencesChanged(Tp::SimpleContactPresences)),
SLOT(onPresencesChanged(Tp::SimpleContactPresences)));
} else if (feature == Contact::FeatureRosterGroups || feature == Contact::FeatureAddresses) {
// nothing to do here, but we don't want to warn
;
} else {
warning() << " Unknown feature" << feature
<< "when trying to figure out how to connect change notification!";
}
mPriv->tracking[feature] = true;
}
PendingOperation *ContactManager::introspectRoster()
{
return mPriv->roster->introspect();
}
PendingOperation *ContactManager::introspectRosterGroups()
{
return mPriv->roster->introspectGroups();
}
void ContactManager::resetRoster()
{
mPriv->roster->reset();
}
/**
* \fn void ContactManager::presencePublicationRequested(const Tp::Contacts &contacts)
*
* Emitted whenever some contacts request for presence publication.
*
* \param contacts A set of contacts which requested presence publication.
*/
/**
* \fn void ContactManager::groupAdded(const QString &group)
*
* Emitted when a new contact list group is created.
*
* \param group The group name.
* \sa allKnownGroups()
*/
/**
* \fn void ContactManager::groupRenamed(const QString &oldGroup, const QString &newGroup)
*
* Emitted when a new contact list group is renamed.
*
* \param oldGroup The old group name.
* \param newGroup The new group name.
* \sa allKnownGroups()
*/
/**
* \fn void ContactManager::groupRemoved(const QString &group)
*
* Emitted when a contact list group is removed.
*
* \param group The group name.
* \sa allKnownGroups()
*/
/**
* \fn void ContactManager::groupMembersChanged(const QString &group,
* const Tp::Contacts &groupMembersAdded,
* const Tp::Contacts &groupMembersRemoved,
* const Tp::Channel::GroupMemberChangeDetails &details)
*
* Emitted whenever some contacts got removed or added from
* a group.
*
* \param group The name of the group that changed.
* \param groupMembersAdded A set of contacts which were added to the group \a group.
* \param groupMembersRemoved A set of contacts which were removed from the group \a group.
* \param details The change details.
* \sa groupContacts()
*/
/**
* \fn void ContactManager::allKnownContactsChanged(const Tp::Contacts &contactsAdded,
* const Tp::Contacts &contactsRemoved,
* const Tp::Channel::GroupMemberChangeDetails &details)
*
* Emitted whenever some contacts got removed or added from
* ContactManager's known contact list. It is useful for monitoring which contacts
* are currently known by ContactManager.
*
* Note that, in some protocols, this signal could stream newly added contacts
* with both presence subscription and publication state set to No. Be sure to watch
* over publication and/or subscription state changes if that is the case.
*
* \param contactsAdded A set of contacts which were added to the known contact list.
* \param contactsRemoved A set of contacts which were removed from the known contact list.
* \param details The change details.
* \sa allKnownContacts()
*/
} // Tp
|