File: SearchResult.cpp

package info (click to toggle)
eiskaltdcpp 2.4.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,676 kB
  • sloc: cpp: 97,597; ansic: 5,004; perl: 1,897; xml: 1,440; sh: 1,313; php: 661; javascript: 257; makefile: 39
file content (90 lines) | stat: -rw-r--r-- 3,241 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "stdinc.h"

#include "SearchResult.h"
#include "UploadManager.h"
#include "Text.h"
#include "User.h"
#include "ClientManager.h"
#include "Client.h"

namespace dcpp {

SearchResult::SearchResult(const UserPtr& aUser, Types aType, int aSlots, int aFreeSlots,
                           int64_t aSize, const string& aFile, const string& aHubName,
                           const string& aHubURL, const string& ip, TTHValue aTTH, const string& aToken) :
    file(aFile), hubName(aHubName), hubURL(aHubURL), user(aUser),
    size(aSize), type(aType), aslots(aSlots), freeSlots(aFreeSlots), IP(ip),
    tth(aTTH), token(aToken) { }

SearchResult::SearchResult(Types aType, int64_t aSize, const string& aFile, const TTHValue& aTTH) :
    file(aFile), user(ClientManager::getInstance()->getMe()), size(aSize), type(aType), aslots(SETTING(SLOTS)),
    freeSlots(UploadManager::getInstance()->getFreeSlots()),
    tth(aTTH) { }

string SearchResult::toSR(const Client& c) const {
    // File:        "$SR %s %s%c%s %d/%d%c%s (%s)|"
    // Directory:   "$SR %s %s %d/%d%c%s (%s)|"
    string tmp;
    tmp.reserve(128);
    tmp.append("$SR ", 4);
    tmp.append(Text::fromUtf8(c.getMyNick(), c.getEncoding()));
    tmp.append(1, ' ');
    string acpFile = Text::fromUtf8(file, c.getEncoding());
    if(type == TYPE_FILE) {
        tmp.append(acpFile);
        tmp.append(1, '\x05');
        tmp.append(Util::toString(size));
    } else {
        tmp.append(acpFile, 0, acpFile.length() - 1);
    }
    tmp.append(1, ' ');
    tmp.append(Util::toString(freeSlots));
    tmp.append(1, '/');
    tmp.append(Util::toString(aslots));
    tmp.append(1, '\x05');
    tmp.append("TTH:" + getTTH().toBase32());
    tmp.append(" (", 2);
    tmp.append(c.getIpPort());
    tmp.append(")|", 2);
    return tmp;
}

AdcCommand SearchResult::toRES(char type) const {
    AdcCommand cmd(AdcCommand::CMD_RES, type);
    cmd.addParam("SI", Util::toString(size));
    cmd.addParam("SL", Util::toString(freeSlots));
    cmd.addParam("FN", Util::toAdcFile(file));
    cmd.addParam("TR", getTTH().toBase32());
    return cmd;
}

// file:      path\to\something  --> "something"
// file:      path\to\something\ --> ""
// directory: path\to\something  --> "something"
// directory: path\to\something\ --> "something"
string SearchResult::getBaseName() const {
    if(getType() == TYPE_FILE)
        return Util::getFileName(getFile(), '\\');

    dcassert(getType() == TYPE_DIRECTORY);
    return Util::getLastDir(getFile(), '\\');
}

}