File indexing completed on 2024-04-21 15:43:00
0001 /* 0002 * This file is part of TelepathyLoggerQt 0003 * 0004 * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/> 0005 * 0006 * This library is free software; you can redistribute it and/or modify 0007 * it under the terms of the GNU Lesser General Public License as published 0008 * by the Free Software Foundation; either version 2.1 of the License, or 0009 * (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Lesser General Public License 0017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "utils.h" 0021 0022 #include <TelepathyQt/AccountManager> 0023 #include <TelepathyQt/Connection> 0024 #include <TelepathyQt/Contact> 0025 #include <TelepathyQt/ContactManager> 0026 #include <TelepathyQt/ReferencedHandles> 0027 #include <telepathy-glib/connection.h> 0028 #include <telepathy-glib/contact.h> 0029 #include <telepathy-glib/dbus.h> 0030 #include <telepathy-glib/proxy.h> 0031 #include <telepathy-glib/simple-client-factory.h> 0032 0033 using namespace Tpl; 0034 0035 Utils *Utils::instance() 0036 { 0037 static Utils *instance = 0; 0038 0039 if (!instance) { 0040 qDebug() << "Created Utils instance"; 0041 instance = new Utils(); 0042 } 0043 0044 return instance; 0045 } 0046 0047 Utils::Utils() 0048 { 0049 } 0050 0051 Utils::~Utils() 0052 { 0053 } 0054 0055 void Utils::setAccountManagerPtr(const Tp::AccountManagerPtr & accountManager) 0056 { 0057 mAccountManagerPtr = accountManager; 0058 } 0059 0060 Tp::AccountManagerPtr Utils::accountManagerPtr() 0061 { 0062 qDebug(); 0063 if (mAccountManagerPtr.isNull()) { 0064 qDebug() << "Created Tp::AccountManager instance"; 0065 mAccountManagerPtr = Tp::AccountManager::create(); 0066 } 0067 0068 return mAccountManagerPtr; 0069 } 0070 0071 TpAccountManager *Utils::tpAccountManager() 0072 { 0073 qDebug(); 0074 return tp_account_manager_dup(); 0075 } 0076 0077 Tp::AccountPtr Utils::accountPtr(TpAccount *account) 0078 { 0079 qDebug() << "account=" << account; 0080 0081 if (!account) { 0082 return Tp::AccountPtr(); 0083 } 0084 0085 const gchar *objectPath = tp_proxy_get_object_path(account); 0086 qDebug() << "objectPath=" << objectPath; 0087 if (!objectPath) { 0088 return Tp::AccountPtr(); 0089 } 0090 0091 return accountPtr(objectPath); 0092 } 0093 0094 Tp::AccountPtr Utils::accountPtr(const QString &objectPath) 0095 { 0096 qDebug() << "objectPath=" << objectPath; 0097 0098 Tp::AccountPtr accountPtr = accountManagerPtr()->accountForPath(objectPath); 0099 0100 qDebug() << "accountPtr=" << accountPtr; 0101 return accountPtr; 0102 } 0103 0104 TpAccount *Utils::tpAccount(const Tp::AccountPtr & accountPtr) 0105 { 0106 qDebug() << "account=" << accountPtr; 0107 0108 QString objectPath = accountPtr->objectPath(); 0109 qDebug() << "objectPath=" << objectPath; 0110 if (objectPath.isEmpty()) { 0111 return 0; 0112 } 0113 0114 GError *error = NULL; 0115 TpAccount * account = tp_simple_client_factory_ensure_account(tp_proxy_get_factory(tpAccountManager()), 0116 objectPath.toUtf8(), 0117 NULL, 0118 &error); 0119 0120 if (account == NULL) { 0121 qDebug() << "failed to create account:" << error->message; 0122 g_clear_error(&error); 0123 return NULL; 0124 } 0125 0126 qDebug() << "account=" << account; 0127 return account; 0128 } 0129 0130 #if 0 0131 TpContact *ToTpContact(Tp::ContactPtr contact) 0132 { 0133 if (contact.isNull()) { 0134 return 0; 0135 } 0136 0137 Tp::ReferencedHandles handles = contact->handle(); 0138 if (handles.isEmpty()) { 0139 return 0; 0140 } 0141 0142 GError *error = 0; 0143 TpDBusDaemon * tpDBusDaemon = tp_dbus_daemon_dup(&error); 0144 if (error) { 0145 g_error_free(error); 0146 return 0; 0147 } 0148 0149 if (!tpDBusDaemon) { 0150 return 0; 0151 } 0152 0153 if (!contact->manager()) { 0154 return 0; 0155 } 0156 0157 Tp::ConnectionPtr connection = contact->manager()->connection(); 0158 if (!connection) { 0159 return 0; 0160 } 0161 0162 QString busName = connection->busName(); 0163 QString objectPath = connection->objectPath(); 0164 TpConnection *tpConnection = tp_connection_new(tpDBusDaemon, busName.toLatin1(), objectPath.toLatin1(), &error); 0165 if (error) { 0166 return 0; 0167 } 0168 0169 if (!tpConnection) { 0170 return 0; 0171 } 0172 0173 Tp::ReferencedHandles::const_iterator it; 0174 for(it = handles.constBegin(); it != handles.constEnd(); it++) { 0175 // todo use async version: tp_connection_get_contacts_by_handle 0176 TpContact *tpContact = tp_connection_dup_contact_if_possible(tpConnection, *it, contact->id().toLatin1()); 0177 if (tpContact) { 0178 return tpContact; 0179 } 0180 } 0181 0182 return 0; 0183 } 0184 0185 Tp::ContactPtr ToTpContactPtr(TpContact * tpContact) 0186 { 0187 if (!tpContact) { 0188 return Tp::ContactPtr(); 0189 } 0190 0191 TpConnection * tpConnection = tp_contact_get_connection(tpContact); 0192 if (!tpConnection) { 0193 return Tp::ContactPtr(); 0194 } 0195 0196 const gchar *busName = tp_proxy_get_bus_name(TP_PROXY(tpConnection)); 0197 if (!busName) { 0198 return Tp::ContactPtr(); 0199 } 0200 0201 const gchar *objectPath = tp_proxy_get_object_path(TP_PROXY(tpConnection)); 0202 if (!objectPath) { 0203 return Tp::ContactPtr(); 0204 } 0205 0206 Tp::ConnectionPtr connection = Tp::Connection::create(busName, objectPath); 0207 if (connection.isNull()) { 0208 return Tp::ContactPtr(); 0209 } 0210 0211 if (connection->contactManager().isNull()) { 0212 return Tp::ContactPtr(); 0213 } 0214 0215 TpHandle handle = tp_contact_get_handle(contact); 0216 // TODO use async version 0217 return connection->contactManager()->lookupContactByHandle(handle); 0218 } 0219 #endif