Upstream tarball 10013
[amule.git] / src / Friend.cpp
blob7390c9166bf1f87fe17a1081067e4090b5247e97
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "Friend.h" // Interface declarations.
28 #include "PartFile.h" // Needed for CPartFile
29 #include "updownclient.h" // Needed for CUpDownClient
30 #include "GuiEvents.h" // Needed for Notify_*
33 CFriend::CFriend()
35 m_dwLastSeen = 0;
36 m_dwLastUsedIP = 0;
37 m_nLastUsedPort = 0;
38 m_dwLastChatted = 0;
39 m_LinkedClient = NULL;
43 CFriend::CFriend( const CMD4Hash& userhash, uint32 tm_dwLastSeen, uint32 tm_dwLastUsedIP, uint32 tm_nLastUsedPort, uint32 tm_dwLastChatted, const wxString& tm_strName)
45 m_dwLastSeen = tm_dwLastSeen;
46 m_dwLastUsedIP = tm_dwLastUsedIP;
47 m_nLastUsedPort = tm_nLastUsedPort;
48 m_dwLastChatted = tm_dwLastChatted;
49 m_UserHash = userhash;
51 if (tm_strName.IsEmpty()) {
52 m_strName = wxT("?");
53 } else {
54 m_strName = tm_strName;
57 m_LinkedClient = NULL;
61 CFriend::CFriend(CUpDownClient* client)
63 wxASSERT( client );
65 LinkClient(client, false); // On init, no need to unlink. BETTER not to unlink.
67 m_dwLastChatted = 0;
71 void CFriend::LinkClient(CUpDownClient* client, bool unlink) {
72 // Link the friend to that client
73 if (unlink && m_LinkedClient) { // What, is already linked?
74 if (m_LinkedClient != client){
75 bool bFriendSlot = m_LinkedClient->GetFriendSlot();
76 // avoid that an unwanted client instance keeps a friend slot
77 m_LinkedClient->SetFriendSlot(false);
78 m_LinkedClient->SetFriend(NULL);
79 m_LinkedClient = client;
80 // move an assigned friend slot between different client instances which are/were also friends
81 m_LinkedClient->SetFriendSlot(bFriendSlot);
83 } else {
84 m_LinkedClient = client;
87 if ( !client->GetUserName().IsEmpty() ) {
88 m_strName = client->GetUserName();
89 } else {
90 m_strName = wxT("?");
92 m_UserHash = client->GetUserHash();
93 m_dwLastUsedIP = client->GetIP();
94 m_nLastUsedPort = client->GetUserPort();
95 m_dwLastSeen = time(NULL);
96 // This will update the Link status also on GUI.
97 Notify_ChatRefreshFriend(this, true);
101 void CFriend::LoadFromFile(CFileDataIO* file)
103 wxASSERT( file );
105 m_UserHash = file->ReadHash();
106 m_dwLastUsedIP = file->ReadUInt32();
107 m_nLastUsedPort = file->ReadUInt16();
108 m_dwLastSeen = file->ReadUInt32();
109 m_dwLastChatted = file->ReadUInt32();
111 uint32 tagcount = file->ReadUInt32();
112 for ( uint32 j = 0; j != tagcount; j++) {
113 CTag newtag(*file, true);
114 switch ( newtag.GetNameID() ) {
115 case FF_NAME:
116 if (m_strName.IsEmpty()) {
117 m_strName = newtag.GetStr();
119 break;
125 void CFriend::WriteToFile(CFileDataIO* file)
127 wxASSERT( file );
128 file->WriteHash(m_UserHash);
129 file->WriteUInt32(m_dwLastUsedIP);
130 file->WriteUInt16(m_nLastUsedPort);
131 file->WriteUInt32(m_dwLastSeen);
132 file->WriteUInt32(m_dwLastChatted);
134 uint32 tagcount = ( m_strName.IsEmpty() ? 0 : 2 );
135 file->WriteUInt32(tagcount);
136 if ( !m_strName.IsEmpty() ) {
137 CTagString nametag(FF_NAME, m_strName);
138 nametag.WriteTagToFile(file, utf8strOptBOM);
139 nametag.WriteTagToFile(file);
144 bool CFriend::HasFriendSlot() {
145 if (GetLinkedClient()) {
146 return GetLinkedClient()->GetFriendSlot();
147 } else {
148 return false;
151 // File_checked_for_headers