contact_manager.cpp

00001 /* 00002 $Id: contact__manager_8cpp-source.html,v 1.1 2004/10/05 21:12:01 mentat Exp $ 00003 00004 GNU Messenger - The secure instant messenger 00005 00006 Copyright (C) 2003-2004 Jesse Lovelace <jesse at aslogicsys dot com> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #include "gm/contact_manager.h" 00024 #include "gm/xml_manager.h" 00025 #include "gm/contact.h" 00026 00027 namespace GNUMessenger 00028 { 00029 00030 void 00031 ContactManager::deleteAllNets(const string& contactname) 00032 { 00033 XMLNode searcher; 00034 00035 if (!XMLManager::search(contactname, "contact", m_xml.child("contacts").child("folder"), searcher)) 00036 { 00037 LOG_THROW("deleteAllNets: InvalidIUserName", InvalidUserName); 00038 return; 00039 } 00040 00041 try { 00042 searcher.delChild("protocols"); 00043 } 00044 catch(XMLNode::InvalidChild &e) 00045 { 00046 LOG_THROW(e.what(), InvalidNode); 00047 return; 00048 } 00049 00050 searcher.addChild("protocols"); 00051 00052 return; 00053 } 00054 00055 void 00056 ContactManager::deleteAllInfo(const string& contactname) 00057 { 00058 XMLNode searcher; 00059 00060 if (!XMLManager::search(contactname, "contact", m_xml.child("contacts").child("folder"), searcher)) 00061 { 00062 LOG_THROW("deleteAllInfo: InvalidUserName", InvalidUserName); 00063 return; 00064 } 00065 00066 try { 00067 searcher.delChild("info"); 00068 } 00069 catch(XMLNode::InvalidChild& e) 00070 { 00071 LOG_THROW(e.what(), InvalidNode); 00072 return; 00073 } 00074 00075 searcher.addChild("info"); 00076 00077 return; 00078 } 00079 00080 string ContactManager::getAvailableName() const 00081 { 00082 unsigned int num = 0; 00083 00084 stringstream name; 00085 do 00086 { 00087 name.flush(); 00088 name << "New User - " << num++; 00089 } 00090 while (contactExists(name.str()) 00091 && (num < 100)); // to avoid infinite loop 00092 00093 if (num == 100) 00094 { 00095 LOG_ERROR("Could not create a temporary user object!"); 00096 return ""; 00097 } 00098 00099 return name.str(); 00100 00101 } 00102 00103 unsigned long 00104 ContactManager::getCount() 00105 { 00106 return XMLManager::tagCount("contact", m_xml.child("contacts").child("folder")); 00107 } 00108 00109 bool 00110 ContactManager::renameContact(const string& oldname, const string& newname) 00111 { 00112 XMLNode searcher; 00113 00114 if (!XMLManager::search(oldname, "contact", m_xml.child("contacts").child("folder"), searcher)) 00115 return false; 00116 00117 if (contactExists(newname)) 00118 return false; 00119 00120 searcher.setProperty("name", newname); 00121 00122 return true; 00123 } 00124 00125 XMLNode 00126 ContactManager::getXML(const string& name) 00127 { 00128 XMLNode temp; 00129 if (!XMLManager::search(name, "contact", m_xml.child("contacts").child("folder"), temp)) 00130 LOG("There is no such contact in the XML tree."); 00131 return temp; 00132 } 00133 00134 Folder 00135 ContactManager::getFolder(const string& name) 00136 { 00137 if (name == "") 00138 { 00139 return Folder(m_xml.child("contacts").child("folder")); 00140 } 00141 else 00142 { 00143 XMLNode temp; 00144 if (!XMLManager::search(name, "folder", m_xml.child("contacts").child("folder"), temp)) 00145 LOG_THROW("There is no such folder in the XML tree.", InvalidNode); 00146 return Folder(temp); 00147 } 00148 } 00149 00150 00151 XMLNode 00152 ContactManager::getFolderXML(const string & name) 00153 { 00154 XMLNode temp; 00155 if (!XMLManager::search(name, "folder", m_xml.child("contacts").child("folder"), temp)) 00156 LOG("There is no such folder in the XML tree."); 00157 return temp; 00158 } 00159 00160 00161 Contact 00162 ContactManager::getContact(const string& username) 00163 { 00164 return Contact(getXML(username)); 00165 } 00166 00167 string 00168 ContactManager::getPublicKey(const string& contactname) 00169 { 00170 XMLNode searcher; 00171 00172 if (!XMLManager::search(contactname, "contact", m_xml.child("contacts").child("folder"), searcher)) 00173 return ""; 00174 00175 return searcher.child("key").property("data"); 00176 } 00177 00178 bool 00179 ContactManager::addContact(const string& contactname, const string& folder) 00180 { 00181 XMLNode searcher; 00182 00183 if (folder == "") 00184 searcher = m_xml.child("contacts").child("folder"); 00185 else 00186 if (!XMLManager::search(folder, "folder", m_xml.child("contacts").child("folder"), searcher)) 00187 return false; 00188 00189 searcher.addChild("contact").setProperty("name", contactname); 00190 return true; 00191 } 00192 00193 bool 00194 ContactManager::addContact(const Contact& c, const string& folder) 00195 { 00196 XMLNode searcher; 00197 00198 if (folder == "") 00199 searcher = m_xml.child("contacts").child("folder"); 00200 else 00201 if (!XMLManager::search(folder, "folder", m_xml.child("contacts").child("folder"), searcher)) 00202 return false; 00203 00204 XMLNode newnode(c); 00205 00206 searcher.addChild(newnode); 00207 00208 return true; 00209 } 00210 00211 bool 00212 ContactManager::deleteContact(const string& contactname) 00213 { 00214 unsigned int num; 00215 XMLNode baseSearch; 00216 00217 if (!XMLManager::baseSearch(contactname, "contact", m_xml.child("contacts").child("folder"), baseSearch, num)) 00218 return false; 00219 00220 Contact(baseSearch.child("contact", num)).Destroy(); 00221 baseSearch.delChild("contact", num); 00222 return true; 00223 } 00224 00225 bool 00226 ContactManager::moveContact(const string& name, const string& newbase) 00227 { 00228 XMLNode searcher; 00229 if (!XMLManager::search(name, "contact", m_xml.child("contacts").child("folder"), searcher)) 00230 return false; 00231 00232 unsigned int num; 00233 XMLNode oldBase; 00234 00235 if (!XMLManager::baseSearch(name, "contact", m_xml.child("contacts").child("folder"), oldBase, num)) 00236 return false; 00237 00238 XMLNode newBase; 00239 00240 if (newbase == "") 00241 newBase = m_xml.child("contacts").child("folder"); 00242 else 00243 if (!XMLManager::search(newbase, "folder", m_xml.child("contacts").child("folder"), newBase)) 00244 return false; 00245 00246 newBase.addChild(searcher); 00247 oldBase.delChild("contact", num); 00248 00249 return true; 00250 } 00251 00252 bool 00253 ContactManager::setInfo(const string& username, const string& infoname, const string& data, const string& childof) 00254 { 00255 // see if this contact exists 00256 XMLNode user; 00257 00258 if (!XMLManager::search(username, "contact", m_xml.child("contacts").child("folder"), user)) 00259 return false; 00260 00261 XMLNode item; 00262 00263 // see if the item exists 00264 if (XMLManager::search(infoname, "item", user.child("info"), item)) 00265 { 00266 item.setProperty("data",data); 00267 return true; 00268 } 00269 00270 // otherwise, see if a parent node was specified 00271 if (childof != "") 00272 { 00273 XMLNode childOfNode; 00274 00275 if (!XMLManager::search(childof, "item", user.child("info"), childOfNode)) 00276 return false; 00277 // the parent node specified does not exist, return false 00278 00279 // the parent node does exist, add this new child and return true 00280 childOfNode.addChild("item").setProperty("name", infoname).setProperty("data",data); 00281 00282 return true; 00283 } 00284 00285 // no parent node specified, just add to base <info/> tag 00286 user.child("info").addChild("item").setProperty("name", infoname).setProperty("data",data); 00287 00288 return true; 00289 } 00290 00291 bool 00292 ContactManager::deleteInfo(const string& username, const string& infoname) 00293 { 00294 XMLNode contact; 00295 00296 if (!XMLManager::search(username, "contact", m_xml.child("contacts").child("folder"), contact)) 00297 return false; 00298 00299 unsigned int num; 00300 00301 XMLNode info; 00302 00303 if (!XMLManager::baseSearch(infoname, "item", contact.child("info"), info, num)) 00304 return false; 00305 00306 info.delChild("item", num); 00307 return true; 00308 } 00309 00310 string 00311 ContactManager::getInfo(const string& username, const string& infoname) 00312 { 00313 XMLNode contact; 00314 00315 if (!XMLManager::search(username, "contact", m_xml.child("contacts").child("folder"), contact)) 00316 return ""; 00317 00318 XMLNode info; 00319 00320 if (!XMLManager::search(infoname, "item", contact.child("info"), info)) 00321 return ""; 00322 00323 return info.property("data"); 00324 } 00325 00326 bool 00327 ContactManager::addNet(const string& username, const string& netname, const string& login) 00328 { 00329 // see if this contact exists 00330 XMLNode user; 00331 00332 if (!XMLManager::search(username, "contact", m_xml.child("contacts").child("folder"), user)) 00333 return false; 00334 00335 // see if the item exists 00336 if (user.child("protocols").hasChild(netname)) 00337 { 00338 LOG_DEBUG("addNet: Found this network, replacing."); 00339 user.child("protocols").child(netname).setProperty("login",login); 00340 return true; 00341 } 00342 00343 LOG_DEBUG("addNet: Network not found, adding."); 00344 user.child("protocols").addChild(netname).setProperty("login",login); 00345 return true; 00346 } 00347 00348 bool 00349 ContactManager::deleteNet(const string& username, const string& netname) 00350 { 00351 XMLNode contact; 00352 00353 if (!XMLManager::search(username, "contact", m_xml.child("contacts").child("folder"), contact)) 00354 { 00355 LOG_THROW("deleteNet: InvalidUserName", InvalidUserName); 00356 return false; 00357 } 00358 00359 if (!contact.child("protocols").hasChild(netname)) 00360 return false; 00361 00362 contact.child("protocols").delChild(netname); 00363 00364 return true; 00365 } 00366 00367 void 00368 ContactManager::getContactsOfProtocol(const string& protocol, vector<Contact>& contacts) 00369 { 00370 contacts.clear(); 00371 00372 vector<XMLNode> allContacts; 00373 XMLManager::getAllTags("contact", m_xml.child("contacts").child("folder"), allContacts); 00374 LOG_DEBUG("ALL SIZE: " << allContacts.size()); 00375 for (unsigned int i = 0; i < allContacts.size(); i++) 00376 { 00377 XMLNode temp(allContacts[i]); 00378 if (temp.child("protocols").hasChild(protocol)) { 00379 LOG_DEBUG("FOUND!"); 00380 Contact c(temp); 00381 contacts.push_back(c); 00382 } 00383 } 00384 } 00385 00386 vector<Contact> 00387 ContactManager::getAllContacts() 00388 { 00389 vector<Contact> ret; 00390 vector<XMLNode> allContacts; 00391 XMLManager::getAllTags("contact", m_xml.child("contacts").child("folder"), allContacts); 00392 00393 for (unsigned int i = 0; i < allContacts.size(); i++) 00394 { 00395 ret.push_back(Contact(allContacts[i])); 00396 } 00397 00398 return ret; 00399 } 00400 00401 bool 00402 ContactManager::renameFolder(const string& oldname, const string& newname) 00403 { 00404 XMLNode fold; 00405 00406 if (!XMLManager::search(oldname, "folder", m_xml.child("contacts").child("folder"), fold)) 00407 { 00408 LOG_DEBUG("renameFolder: InvalidUserName"); 00409 return false; 00410 } 00411 fold.setProperty("name", newname); 00412 00413 return true; 00414 } 00415 00416 bool 00417 ContactManager::addFolder(const string& folder_name, const string& base) 00418 { 00419 if (folderExists(folder_name)) 00420 { 00421 LOG_THROW("addFolder: InvalidFolder", InvalidFolder); 00422 return false; 00423 } 00424 00425 XMLNode searcher; 00426 00427 // if no base given search from root 00428 if (base == "") 00429 searcher = m_xml.child("contacts").child("folder"); 00430 else 00431 if (!XMLManager::search(base, "folder", m_xml.child("contacts").child("folder"), searcher)) 00432 return false; 00433 00434 searcher.addChild("folder").setProperty("name", folder_name); 00435 00436 return true; 00437 } 00438 00439 bool 00440 ContactManager::deleteFolder(const string& folder_name) 00441 { 00442 unsigned int num; 00443 XMLNode base; 00444 00445 LOG_DEBUG("WARNING THIS DOESNT 'DESTROY' CHILDREN"); 00447 00448 if (!XMLManager::baseSearch(folder_name, "folder", m_xml.child("contacts").child("folder"), base, num)) 00449 { 00450 LOG_DEBUG("deleteFolder: InvalidFolder"); 00451 return false; 00452 } 00453 00454 base.delChild("folder", num); 00455 return true; 00456 } 00457 00458 bool 00459 ContactManager::moveFolder(const string& name, const string& newbase) 00460 { 00461 unsigned int num; 00462 00463 XMLNode origbase; 00464 00465 if (!XMLManager::baseSearch(name, "folder", m_xml.child("contacts").child("folder"), origbase, num)) 00466 { 00467 LOG_THROW("moveFolder: InvalidFolder", InvalidFolder); 00468 return false; 00469 } 00470 // folder of that name not found 00471 00472 XMLNode tobase; 00473 00474 if (!XMLManager::search(name, "folder", m_xml.child("contacts").child("folder"), tobase)) 00475 return false; 00476 00477 tobase.addChild(origbase.child("folder", num)); 00478 00479 origbase.delChild("folder", num); 00480 00481 return true; 00482 00483 } 00484 00485 bool 00486 ContactManager::folderExists(const string& name) const 00487 { 00488 XMLNode temp; 00489 00490 if (XMLManager::search(name, "folder", m_xml.child("contacts").child("folder"), temp)) 00491 return true; 00492 return false; 00493 00494 } 00495 00496 bool 00497 ContactManager::contactExists(const string& name) const 00498 { 00499 00500 XMLNode temp; 00501 if (XMLManager::search(name, "contact", m_xml.child("contacts").child("folder"), temp)) 00502 return true; 00503 00504 return false; 00505 } 00506 00507 bool 00508 ContactManager::getInfoXML(const string& username, XMLNode& xml) 00509 { 00510 XMLNode contact; 00511 00512 if (!XMLManager::search(username, "contact", m_xml.child("contacts").child("folder"), contact)) 00513 { 00514 LOG_DEBUG("getInfoXML: InvalidUserName"); 00515 return false; 00516 } 00517 00518 try { 00519 xml = contact.child("info"); 00520 } catch (XMLNode::InvalidChild& e) { 00521 LOG_DEBUG(e.what()); 00522 return false; 00523 } 00524 00525 return true; 00526 } 00527 00528 bool 00529 ContactManager::setInfoXML(const string& username, const XMLNode& xml) 00530 { 00531 XMLNode contact; 00532 00533 if (!XMLManager::search(username, "contact", m_xml.child("contacts").child("folder"), contact)) 00534 { 00535 LOG_DEBUG("setInfoXML: InvalidUserName"); 00536 return false; 00537 } 00538 00539 try { 00540 contact.child("info") = xml; 00541 } 00542 catch (XMLNode::InvalidChild& e) 00543 { 00544 LOG_ERROR(e.what()); 00545 return false; 00546 } 00547 00548 return true; 00549 } 00550 00551 } // !GNUMessenger 00552

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8