Fix bad #define
[amule.git] / src / Friend.cpp
blob3e528fcfd3b1ec1550c8529ffe23d7bd918e4eb3
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 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.
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 "SafeFile.h" // Needed for CFileDataIO
29 #include "GuiEvents.h" // Needed for Notify_*
32 void CFriend::Init()
34 m_dwLastSeen = 0;
35 m_dwLastUsedIP = 0;
36 m_nLastUsedPort = 0;
37 m_dwLastChatted = 0;
41 CFriend::CFriend( const CMD4Hash& userhash, uint32 tm_dwLastSeen, uint32 tm_dwLastUsedIP, uint32 tm_nLastUsedPort, uint32 tm_dwLastChatted, const wxString& tm_strName)
43 m_dwLastSeen = tm_dwLastSeen;
44 m_dwLastUsedIP = tm_dwLastUsedIP;
45 m_nLastUsedPort = tm_nLastUsedPort;
46 m_dwLastChatted = tm_dwLastChatted;
47 m_UserHash = userhash;
49 if (tm_strName.IsEmpty()) {
50 m_strName = wxT("?");
51 } else {
52 m_strName = tm_strName;
57 CFriend::CFriend(CClientRef client)
59 LinkClient(client);
61 m_dwLastChatted = 0;
65 void CFriend::LinkClient(CClientRef client)
67 wxASSERT(client.IsLinked());
69 // Link the friend to that client
70 if (m_LinkedClient != client) { // do nothing if already linked to this client
71 bool hadFriendslot = false;
72 if (m_LinkedClient.IsLinked()) { // What, is already linked?
73 hadFriendslot = m_LinkedClient.GetFriendSlot();
74 UnLinkClient(false);
76 m_LinkedClient = client;
77 m_LinkedClient.SetFriend(this);
78 if (hadFriendslot) {
79 // move an assigned friend slot between different client instances which are/were also friends
80 m_LinkedClient.SetFriendSlot(true);
84 // always update (even if client stays the same)
85 if ( !client.GetUserName().IsEmpty() ) {
86 m_strName = client.GetUserName();
87 } else if (m_strName.IsEmpty()) {
88 m_strName = wxT("?");
90 m_UserHash = client.GetUserHash();
91 m_dwLastUsedIP = client.GetIP();
92 m_nLastUsedPort = client.GetUserPort();
93 m_dwLastSeen = time(NULL);
94 // This will update the Link status also on GUI.
95 Notify_ChatUpdateFriend(this);
99 void CFriend::UnLinkClient(bool notify)
101 if (m_LinkedClient.IsLinked()) {
102 // avoid that an unwanted client instance keeps a friend slot
103 if (m_LinkedClient.GetFriendSlot()) {
104 m_LinkedClient.SetFriendSlot(false);
105 CoreNotify_Upload_Resort_Queue();
107 m_LinkedClient.SetFriend(NULL);
108 m_LinkedClient.Unlink();
109 if (notify) {
110 Notify_ChatUpdateFriend(this);
116 void CFriend::LoadFromFile(CFileDataIO* file)
118 wxASSERT( file );
120 m_UserHash = file->ReadHash();
121 m_dwLastUsedIP = file->ReadUInt32();
122 m_nLastUsedPort = file->ReadUInt16();
123 m_dwLastSeen = file->ReadUInt32();
124 m_dwLastChatted = file->ReadUInt32();
126 uint32 tagcount = file->ReadUInt32();
127 for ( uint32 j = 0; j != tagcount; j++) {
128 CTag newtag(*file, true);
129 switch ( newtag.GetNameID() ) {
130 case FF_NAME:
131 if (m_strName.IsEmpty()) {
132 m_strName = newtag.GetStr();
134 break;
140 void CFriend::WriteToFile(CFileDataIO* file)
142 wxASSERT( file );
143 file->WriteHash(m_UserHash);
144 file->WriteUInt32(m_dwLastUsedIP);
145 file->WriteUInt16(m_nLastUsedPort);
146 file->WriteUInt32(m_dwLastSeen);
147 file->WriteUInt32(m_dwLastChatted);
149 uint32 tagcount = ( m_strName.IsEmpty() ? 0 : 2 );
150 file->WriteUInt32(tagcount);
151 if ( !m_strName.IsEmpty() ) {
152 CTagString nametag(FF_NAME, m_strName);
153 nametag.WriteTagToFile(file, utf8strOptBOM);
154 nametag.WriteTagToFile(file);
159 bool CFriend::HasFriendSlot() {
160 if (m_LinkedClient.IsLinked()) {
161 return m_LinkedClient.GetFriendSlot();
162 } else {
163 return false;
166 // File_checked_for_headers