root / modules / gadu_protocol / helpers / gadu-importer.cpp @ 946ac1c4
History | View | Annotate | Download (5.3 kB)
| 1 | /***************************************************************************
|
|---|---|
| 2 | * * |
| 3 | * This program is free software; you can redistribute it and/or modify * |
| 4 | * it under the terms of the GNU General Public License as published by * |
| 5 | * the Free Software Foundation; either version 2 of the License, or * |
| 6 | * (at your option) any later version. * |
| 7 | * * |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include "accounts/account.h" |
| 11 | #include "accounts/account-manager.h" |
| 12 | #include "configuration/configuration-file.h" |
| 13 | #include "configuration/xml-configuration-file.h" |
| 14 | #include "contacts/contact-manager.h" |
| 15 | #include "contacts/contact.h" |
| 16 | #include "contacts/ignored-helper.h" |
| 17 | #include "contacts/account-data/contact-account-data-manager.h" |
| 18 | #include "protocols/protocols-manager.h" |
| 19 | #include "misc/misc.h" |
| 20 | #include "gadu-account-details.h" |
| 21 | #include "gadu-contact-account-data.h" |
| 22 | #include "gadu-protocol-factory.h" |
| 23 | |
| 24 | #include "gadu-importer.h" |
| 25 | |
| 26 | GaduImporter * GaduImporter::Instance; |
| 27 | |
| 28 | GaduImporter * GaduImporter::instance() |
| 29 | {
|
| 30 | if (0 == Instance) |
| 31 | Instance = new GaduImporter();
|
| 32 | |
| 33 | return Instance;
|
| 34 | } |
| 35 | |
| 36 | void GaduImporter::importAccounts()
|
| 37 | {
|
| 38 | if (0 == config_file.readNumEntry("General", "UIN")) |
| 39 | return;
|
| 40 | |
| 41 | Account defaultGaduGadu; |
| 42 | GaduAccountDetails *accountDetails = new GaduAccountDetails(defaultGaduGadu.storage(), defaultGaduGadu);
|
| 43 | defaultGaduGadu.setDetails(accountDetails); |
| 44 | |
| 45 | defaultGaduGadu.setName("Gadu-Gadu");
|
| 46 | defaultGaduGadu.setId(config_file.readEntry("General", "UIN")); |
| 47 | defaultGaduGadu.setPassword(unicode2cp(pwHash(config_file.readEntry("General", "Password")))); |
| 48 | defaultGaduGadu.setRememberPassword(true);
|
| 49 | accountDetails->setAllowDcc(config_file.readBoolEntry("Network", "AllowDCC")); |
| 50 | |
| 51 | QHostAddress host; |
| 52 | if (!host.setAddress(config_file.readEntry("Network", "DccIP"))) |
| 53 | host.setAddress("0.0.0.0");
|
| 54 | accountDetails->setDccIP(host); |
| 55 | if (!host.setAddress(config_file.readEntry("Network", "ExternalIP"))) |
| 56 | host.setAddress("0.0.0.0");
|
| 57 | accountDetails->setDccExternalIP(host); |
| 58 | |
| 59 | accountDetails->setDccExternalPort(config_file.readNumEntry("Network", "ExternalPort")); |
| 60 | accountDetails->setDccPort(config_file.readNumEntry("Network", "ExternalPort")); |
| 61 | accountDetails->setDccIpDetect(config_file.readBoolEntry("Network", "DccIpDetect")); |
| 62 | accountDetails->setDccLocalPort(config_file.readNumEntry("Network", "LocalPort")); |
| 63 | accountDetails->setDccForwarding(config_file.readBoolEntry("Network", "DccForwarding")); |
| 64 | accountDetails->setRemoveCompletedTransfers(config_file.readBoolEntry("Network", "RemoveCompletedTransfers")); |
| 65 | |
| 66 | defaultGaduGadu.setUseProxy(config_file.readBoolEntry("Network", "UseProxy")); |
| 67 | if (!host.setAddress(config_file.readEntry("Network", "ProxyHost"))) |
| 68 | host.setAddress("0.0.0.0");
|
| 69 | defaultGaduGadu.setProxyHost(host); |
| 70 | defaultGaduGadu.setProxyPassword(config_file.readEntry("Network", "ProxyPassword")); |
| 71 | defaultGaduGadu.setProxyPort(config_file.readNumEntry("Network", "ProxyPort")); |
| 72 | defaultGaduGadu.setProxyUser(config_file.readEntry("Network", "ProxyUser")); |
| 73 | defaultGaduGadu.setProxyRequiresAuthentication(!defaultGaduGadu.proxyUser().isEmpty()); |
| 74 | |
| 75 | accountDetails->import_0_6_5_LastStatus(); |
| 76 | |
| 77 | AccountManager::instance()->registerAccount(defaultGaduGadu); |
| 78 | } |
| 79 | |
| 80 | void GaduImporter::importContacts()
|
| 81 | {
|
| 82 | connect(ContactManager::instance(), SIGNAL(contactAdded(Contact &)), |
| 83 | this, SLOT(contactAdded(Contact &)));
|
| 84 | |
| 85 | foreach (Contact contact, ContactManager::instance()->contacts()) |
| 86 | contactAdded(contact); |
| 87 | |
| 88 | importIgnored(); |
| 89 | } |
| 90 | |
| 91 | void GaduImporter::importGaduContact(Contact& contact)
|
| 92 | {
|
| 93 | Account account = AccountManager::instance()->defaultAccount(); |
| 94 | QString id = contact.customData()["uin"];
|
| 95 | |
| 96 | GaduContactAccountData *gcad = new GaduContactAccountData(account, contact, id, true); |
| 97 | |
| 98 | gcad->setBlocked(QVariant(contact.customData()["blocking"]).toBool());
|
| 99 | gcad->setOfflineTo(QVariant(contact.customData()["offline_to"]).toBool());
|
| 100 | |
| 101 | contact.customData().remove("uin");
|
| 102 | contact.customData().remove("blocking");
|
| 103 | contact.customData().remove("offline_to");
|
| 104 | |
| 105 | contact.addAccountData(gcad); |
| 106 | |
| 107 | ContactAccountDataManager::instance()->addContactAccountData(gcad); |
| 108 | } |
| 109 | |
| 110 | void GaduImporter::importIgnored()
|
| 111 | {
|
| 112 | Account account = AccountManager::instance()->defaultAccount(); |
| 113 | if (account.isNull())
|
| 114 | return;
|
| 115 | |
| 116 | QDomElement ignored = xml_config_file->getNode("Ignored", XmlConfigFile::ModeFind);
|
| 117 | if (ignored.isNull())
|
| 118 | return;
|
| 119 | |
| 120 | QDomNodeList ignoredGroups = xml_config_file->getNodes(ignored, "IgnoredGroup");
|
| 121 | for (int i = 0; i < ignoredGroups.count(); i++) |
| 122 | {
|
| 123 | QDomElement ignoredGroup = ignoredGroups.item(i).toElement(); |
| 124 | if (ignoredGroup.isNull())
|
| 125 | continue;
|
| 126 | |
| 127 | ContactSet ignoredList; |
| 128 | QDomNodeList ignoredContacts = xml_config_file->getNodes(ignoredGroup, "IgnoredContact");
|
| 129 | for (int j = 0; j < ignoredContacts.count(); j++) |
| 130 | {
|
| 131 | QDomElement ignoredContact = ignoredContacts.item(j).toElement(); |
| 132 | if (ignoredContact.isNull())
|
| 133 | continue;
|
| 134 | |
| 135 | ignoredList.insert(ContactManager::instance()->byId(account, ignoredContact.attribute("uin")));
|
| 136 | } |
| 137 | |
| 138 | if (0 == ignoredList.count()) |
| 139 | continue;
|
| 140 | |
| 141 | IgnoredHelper::setIgnored(ignoredList); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void GaduImporter::contactAdded(Contact &contact)
|
| 146 | {
|
| 147 | if (contact.customData().contains("uin")) |
| 148 | importGaduContact(contact); |
| 149 | } |