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.
10//
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.
15//
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
24//
25
26
27#include "Friend.h"			// Interface declarations.
28#include "SafeFile.h"		// Needed for CFileDataIO
29#include "GuiEvents.h"		// Needed for Notify_*
30
31
32void CFriend::Init()
33{
34	m_dwLastSeen = 0;
35	m_dwLastUsedIP = 0;
36	m_nLastUsedPort = 0;
37	m_dwLastChatted = 0;
38}
39
40
41CFriend::CFriend( const CMD4Hash& userhash, uint32 tm_dwLastSeen, uint32 tm_dwLastUsedIP, uint32 tm_nLastUsedPort, uint32 tm_dwLastChatted, const wxString& tm_strName)
42{
43	m_dwLastSeen = tm_dwLastSeen;
44	m_dwLastUsedIP = tm_dwLastUsedIP;
45	m_nLastUsedPort = tm_nLastUsedPort;
46	m_dwLastChatted = tm_dwLastChatted;
47	m_UserHash = userhash;
48
49	if (tm_strName.IsEmpty()) {
50		m_strName = wxT("?");
51	} else {
52		m_strName = tm_strName;
53	}
54}
55
56
57CFriend::CFriend(CClientRef client)
58{
59	LinkClient(client);
60
61	m_dwLastChatted = 0;
62}
63
64
65void CFriend::LinkClient(CClientRef client)
66{
67	wxASSERT(client.IsLinked());
68
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);
75		}
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);
81		}
82	}
83
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("?");
89	}
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);
96}
97
98
99void CFriend::UnLinkClient(bool notify)
100{
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();
106		}
107		m_LinkedClient.SetFriend(NULL);
108		m_LinkedClient.Unlink();
109		if (notify) {
110			Notify_ChatUpdateFriend(this);
111		}
112	}
113}
114
115
116void CFriend::LoadFromFile(CFileDataIO* file)
117{
118	wxASSERT( file );
119
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();
125
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();
133				}
134				break;
135		}
136	}
137}
138
139
140void CFriend::WriteToFile(CFileDataIO* file)
141{
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);
148
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);
155	}
156}
157
158
159bool CFriend::HasFriendSlot() {
160	if (m_LinkedClient.IsLinked()) {
161		return m_LinkedClient.GetFriendSlot();
162	} else {
163		return false;
164	}
165}
166// File_checked_for_headers
167