root / modules / gadu_protocol / services / gadu-search-service.cpp @ ddf174a9
History | View | Annotate | Download (4.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 "debug.h" |
| 11 | #include "misc/misc.h" |
| 12 | |
| 13 | #include "socket-notifiers/gadu-protocol-socket-notifiers.h" |
| 14 | #include "gadu-contact-account-data.h" |
| 15 | #include "gadu-protocol.h" |
| 16 | |
| 17 | #include "gadu-search-service.h" |
| 18 | |
| 19 | GaduSearchService::GaduSearchService(GaduProtocol *protocol) : |
| 20 | SearchService(protocol), Protocol(protocol), Query(ContactSearchCriteria()), |
| 21 | SearchSeq(0), From(0), Stopped(false) |
| 22 | {
|
| 23 | } |
| 24 | |
| 25 | void GaduSearchService::searchFirst(ContactSearchCriteria criteria)
|
| 26 | {
|
| 27 | Query = criteria; |
| 28 | From = Query.SearchContact.hasAccountData(Protocol->account()) ? Query.SearchContact.accountData(Protocol->account())->id().toUInt() : 0;
|
| 29 | searchNext(); |
| 30 | } |
| 31 | |
| 32 | void GaduSearchService::searchNext()
|
| 33 | {
|
| 34 | Stopped = false;
|
| 35 | gg_pubdir50_t req = gg_pubdir50_new(GG_PUBDIR50_SEARCH); |
| 36 | |
| 37 | if (Query.SearchContact.hasAccountData(Protocol->account()))
|
| 38 | gg_pubdir50_add(req, GG_PUBDIR50_UIN, (const char *)unicode2cp(Query.SearchContact.accountData(Protocol->account())->id()).data()); |
| 39 | if (!Query.SearchContact.firstName().isEmpty())
|
| 40 | gg_pubdir50_add(req, GG_PUBDIR50_FIRSTNAME, (const char *)unicode2cp(Query.SearchContact.firstName()).data()); |
| 41 | if (!Query.SearchContact.lastName().isEmpty())
|
| 42 | gg_pubdir50_add(req, GG_PUBDIR50_LASTNAME, (const char *)unicode2cp(Query.SearchContact.lastName()).data()); |
| 43 | if (!Query.SearchContact.nickName().isEmpty())
|
| 44 | gg_pubdir50_add(req, GG_PUBDIR50_NICKNAME, (const char *)unicode2cp(Query.SearchContact.nickName()).data()); |
| 45 | if (!Query.SearchContact.city().isEmpty())
|
| 46 | gg_pubdir50_add(req, GG_PUBDIR50_CITY, (const char *)unicode2cp(Query.SearchContact.city()).data()); |
| 47 | if (!Query.BirthYearFrom.isEmpty())
|
| 48 | {
|
| 49 | QString bufYear = Query.BirthYearFrom + ' ' + Query.BirthYearTo;
|
| 50 | gg_pubdir50_add(req, GG_PUBDIR50_BIRTHYEAR, (const char *)unicode2cp(bufYear).data()); |
| 51 | } |
| 52 | switch (Query.SearchContact.gender())
|
| 53 | {
|
| 54 | case ContactData::GenderMale:
|
| 55 | gg_pubdir50_add(req, GG_PUBDIR50_GENDER, GG_PUBDIR50_GENDER_MALE); |
| 56 | break;
|
| 57 | case ContactData::GenderFemale:
|
| 58 | gg_pubdir50_add(req, GG_PUBDIR50_GENDER, GG_PUBDIR50_GENDER_FEMALE); |
| 59 | break;
|
| 60 | } |
| 61 | |
| 62 | if (Query.Active)
|
| 63 | gg_pubdir50_add(req, GG_PUBDIR50_ACTIVE, GG_PUBDIR50_ACTIVE_TRUE); |
| 64 | |
| 65 | gg_pubdir50_add(req, GG_PUBDIR50_START, qPrintable(QString::number(From))); |
| 66 | |
| 67 | SearchSeq = gg_pubdir50(Protocol->gaduSession(), req); |
| 68 | gg_pubdir50_free(req); |
| 69 | } |
| 70 | |
| 71 | void GaduSearchService::stop()
|
| 72 | {
|
| 73 | Stopped = true;
|
| 74 | } |
| 75 | |
| 76 | void GaduSearchService::handleEventPubdir50SearchReply(struct gg_event *e) |
| 77 | {
|
| 78 | gg_pubdir50_t res = e->event.pubdir50; |
| 79 | |
| 80 | ContactList results; |
| 81 | |
| 82 | int count = gg_pubdir50_count(res);
|
| 83 | kdebugmf(KDEBUG_NETWORK|KDEBUG_INFO, "found %d results\n", count);
|
| 84 | |
| 85 | for (int i = 0; i < count; i++) |
| 86 | {
|
| 87 | Contact result; |
| 88 | |
| 89 | GaduContactAccountData *gcad = new GaduContactAccountData(Protocol->account(), result,
|
| 90 | gg_pubdir50_get(res, i, GG_PUBDIR50_UIN)); |
| 91 | //TODO 0.6.6
|
| 92 | Status status; |
| 93 | status.setType(Protocol->statusTypeFromGaduStatus(atoi(gg_pubdir50_get(res, i, GG_PUBDIR50_STATUS)) & 127));
|
| 94 | gcad->setStatus(status); |
| 95 | result.addAccountData(gcad); |
| 96 | |
| 97 | result.setFirstName(cp2unicode(gg_pubdir50_get(res, i, GG_PUBDIR50_FIRSTNAME))); |
| 98 | result.setLastName(cp2unicode(gg_pubdir50_get(res, i, GG_PUBDIR50_LASTNAME))); |
| 99 | result.setNickName(cp2unicode(gg_pubdir50_get(res, i, GG_PUBDIR50_NICKNAME))); |
| 100 | result.setBirthYear(QString::fromAscii(gg_pubdir50_get(res, i, GG_PUBDIR50_BIRTHYEAR)).toUShort()); |
| 101 | result.setCity(cp2unicode(gg_pubdir50_get(res, i, GG_PUBDIR50_CITY))); |
| 102 | result.setFamilyName(cp2unicode(gg_pubdir50_get(res, i, GG_PUBDIR50_FAMILYNAME))); |
| 103 | result.setFamilyCity(cp2unicode(gg_pubdir50_get(res, i, GG_PUBDIR50_FAMILYCITY))); |
| 104 | result.setGender((ContactData::ContactGender)QString::fromAscii(gg_pubdir50_get(res, i, GG_PUBDIR50_GENDER)).toUShort()); |
| 105 | |
| 106 | results.append(result); |
| 107 | } |
| 108 | |
| 109 | From = gg_pubdir50_next(res); |
| 110 | |
| 111 | emit newResults(results); |
| 112 | } |