4 @implementation amuleFile
6 @synthesize name = m_name;
7 @synthesize size = m_size;
10 return m_hash.lo ^ m_hash.hi;
14 NSString *s = [NSString stringWithFormat:@"%qx%qx", m_hash.hi, m_hash.lo];
18 - (BOOL)isEqual: (id) object {
19 if ([object isKindOfClass:[amuleFile class]]) {
20 amuleFile *myobj = (amuleFile *)object;
21 return ((myobj->m_hash.lo == m_hash.lo) && (myobj->m_hash.hi == m_hash.hi));
27 - (NSString *)convertWithPrefix:(uint64_t)number {
30 if ( number < 1024 ) { // bytes
31 res = [NSString stringWithFormat:@"%llu bytes", number];
32 } else if ( number < 1024*1024 ) { // K
33 res = [NSString stringWithFormat:@"%0.2f Kb", dnum / 1024.0];
34 } else if ( number < 1024*1024*1024 ) { // M
35 res = [NSString stringWithFormat:@"%.2f Mb", dnum / 1048576.0];
37 res = [NSString stringWithFormat:@"%.2f Gb", dnum / 1073741824.0];
45 @implementation DownloadingFile
47 @synthesize src_count = m_src_count;
48 @synthesize non_current_src_count = m_non_current_src_count;
49 @synthesize xfer_src_count = m_xfer_src_count;
50 @synthesize a4af_src_count = m_a4af_src_count;
52 @synthesize speed = m_speed;
54 @synthesize size_done = m_size_done;
55 @synthesize size_xfer = m_size_xfer;
57 @synthesize prio = m_prio;
62 return [self prioToString: m_prio];
66 + (id)createFromEC: (ECTagMD5 *) tag {
67 DownloadingFile *obj = [[DownloadingFile alloc] init];
69 obj->m_hash = [tag getMD5Data];
71 ECTag *nametag = [tag tagByName: EC_TAG_PARTFILE_NAME];
72 ECTagString *stag = (ECTagString *)nametag;
73 obj->m_name = stag.stringValue;;
75 obj->m_size = [tag tagInt64ByName: EC_TAG_PARTFILE_SIZE_FULL];
76 NSLog(@"[EC]: added partfile [%@]\n", obj->m_name);
77 [obj updateFromEC:tag];
82 - (void)updateFromEC:(ECTagMD5 *) tag {
83 NSLog(@"[EC]: updating partfile [%@]\n", m_name);
85 m_size_done = [tag tagInt64ByName: EC_TAG_PARTFILE_SIZE_DONE];
86 m_size_xfer = [tag tagInt64ByName:EC_TAG_PARTFILE_SIZE_XFER];
88 m_speed = [tag tagInt64ByName:EC_TAG_PARTFILE_SPEED];
90 m_src_count = [tag tagInt64ByName: EC_TAG_PARTFILE_SOURCE_COUNT];
91 m_non_current_src_count = [tag tagInt64ByName: EC_TAG_PARTFILE_SOURCE_COUNT_NOT_CURRENT];
92 m_xfer_src_count = [tag tagInt64ByName: EC_TAG_PARTFILE_SOURCE_COUNT_XFER];
93 m_a4af_src_count = [tag tagInt64ByName: EC_TAG_PARTFILE_SOURCE_COUNT_A4AF];
95 m_prio = [tag tagInt64ByName:EC_TAG_PARTFILE_PRIO];
104 - (NSString *)prioToString:(int)prio {
132 s = [s stringByAppendingString:@" (auto)"];
140 @implementation SearchFile
142 @synthesize src_count = m_src_count;
143 @synthesize complete_src_count = m_complete_src_count;
145 + (id)createFromEC: (ECTagMD5 *) tag {
146 SearchFile *obj = [[SearchFile alloc] init];
148 obj->m_hash = [tag getMD5Data];
150 ECTag *nametag = [tag tagByName: EC_TAG_PARTFILE_NAME];
151 ECTagString *stag = (ECTagString *)nametag;
152 obj->m_name = stag.stringValue;;
154 obj->m_size = [tag tagInt64ByName: EC_TAG_PARTFILE_SIZE_FULL];
156 obj->m_known = ([tag tagByName: EC_TAG_KNOWNFILE] == nil) ? false : true;
158 [obj updateFromEC:tag];
163 - (void)updateFromEC:(ECTagMD5 *) tag {
164 m_src_count = [tag tagInt64ByName: EC_TAG_PARTFILE_SOURCE_COUNT];
165 m_complete_src_count = [tag tagInt64ByName: EC_TAG_PARTFILE_SOURCE_COUNT_XFER];
170 @implementation SharedFile
172 @synthesize size_xfer = m_size_xfer;
173 @synthesize size_xfer_all = m_size_xfer_all;
175 @synthesize req_count = m_req_count;
176 @synthesize req_count_all = m_req_count_all;
178 @synthesize accept_count = m_accept_count;
179 @synthesize accept_count_all = m_accept_count_all;
181 @synthesize prio = m_prio;
185 - (NSString *)sprio {
186 return [self prioToString: m_prio];
189 + (id)createFromEC: (ECTagMD5 *) tag {
190 SharedFile *obj = [[SharedFile alloc] init];
192 obj->m_hash = [tag getMD5Data];
194 ECTag *nametag = [tag tagByName: EC_TAG_PARTFILE_NAME];
195 ECTagString *stag = (ECTagString *)nametag;
196 obj->m_name = stag.stringValue;;
198 obj->m_size = [tag tagInt64ByName: EC_TAG_PARTFILE_SIZE_FULL];
200 [obj updateFromEC:tag];
205 - (NSString *)prioToString:(int)prio {
233 s = [s stringByAppendingString:@" (auto)"];
239 - (void)updateFromEC:(ECTagMD5 *) tag {
240 m_req_count = [tag tagInt64ByName: EC_TAG_KNOWNFILE_REQ_COUNT];
241 m_req_count_all = [tag tagInt64ByName: EC_TAG_KNOWNFILE_REQ_COUNT_ALL];
242 m_accept_count = [tag tagInt64ByName: EC_TAG_KNOWNFILE_ACCEPT_COUNT];
243 m_accept_count_all = [tag tagInt64ByName: EC_TAG_KNOWNFILE_ACCEPT_COUNT_ALL];
245 m_size_xfer = [tag tagInt64ByName: EC_TAG_KNOWNFILE_XFERRED];
246 m_size_xfer_all = [tag tagInt64ByName: EC_TAG_KNOWNFILE_XFERRED_ALL];
248 m_prio = [tag tagInt64ByName:EC_TAG_PARTFILE_PRIO];
260 @implementation amuleFileSet
263 if ((self = [super init])) {
264 m_file_dict = [NSMutableDictionary dictionaryWithCapacity:256];
265 m_file_array = [[NSMutableArray alloc] init];
271 return [m_file_dict count];
274 - (void)insertObject:(id)object {
275 id objKey = [object key];
276 [m_file_dict setObject:object forKey:objKey];
277 [m_file_array addObject:object];
283 if ([m_gui_controller respondsToSelector:@selector(reload:)]) {
284 [m_gui_controller performSelector:@selector(reload:)];
286 NSLog(@"Internal error: gui controller doesnt respond to 'reload'\n");
288 // if ([m_gui_controller respondsToSelector:[m_gui_controller reload]) {
289 // [m_gui_controller performSelector:@selector(reload)];
291 // NSLog(@"Internal error: gui controller doesnt respond to 'reload'\n");
295 - (id)objectAtIndex:(int)index {
296 return [m_file_array objectAtIndex:index];
299 - (id)objectForKey:(id)key {
300 return [m_file_dict objectForKey:key];
303 - (void)removeAtIndex:(int)index {
304 id curr_obj = [m_file_array objectAtIndex:index];
305 id key = [curr_obj key];
306 [m_file_dict removeObjectForKey:key];
307 [m_file_array removeObjectAtIndex:index];
312 - (void)removeAtKey:(id)key {
313 [m_file_dict removeObjectForKey:key];
315 for (int i = 0; i < [m_file_array count]; i++) {
316 id curr_obj = [m_file_array objectAtIndex:i];
317 if ([curr_obj key] == key) {
323 [m_file_array removeObjectAtIndex:index];
325 NSLog(@"Internal error: object not found in array\n");
331 - (void)setGuiController:(id)controller {
332 m_gui_controller = controller;
337 @implementation amuleData
339 @synthesize downloads = m_downloads;
340 @synthesize shared = m_shared;
341 @synthesize search_resuls = m_search_results;
343 + (id)initWithConnection:(ECRemoteConnection *) connection {
344 amuleData *obj = [[amuleData alloc] init];
345 connection.delegate = obj;
347 obj->m_connection = connection;
349 obj->m_downloads = [[amuleFileSet alloc] init];
350 obj->m_shared = [[amuleFileSet alloc] init];
351 obj->m_search_results = [[amuleFileSet alloc] init];
356 - (void)handlePacket:(ECPacket *) packet {
357 switch(packet.opcode) {
361 case EC_OP_DLOAD_QUEUE: {
362 [self handleDownloadQueueUpdate: packet];
365 case EC_OP_SHARED_FILES: {
366 [self handleSharedFilesUpdate: packet];
369 case EC_OP_SEARCH_RESULTS: {
370 [self handleSearchUpdate: packet];
374 NSLog(@"[EC] packet with opcode %d not handled\n", packet.opcode);
379 - (void)handleError {
380 NSRunCriticalAlertPanel(@"Daemon communication error",
381 @"Connection with core daemon (amuled) failed",
386 - (void)handleDownloadQueueUpdate:(ECPacket *) packet {
387 for (ECTag *t in packet.subtags) {
388 if ( [t isKindOfClass:[ECTagMD5 class]] ) {
389 ECTagMD5 *tag = (ECTagMD5 *)t;
390 if ( [tag tagCount] == 0 ) {
392 // Only hash here - indication to remove the object
394 [m_downloads removeAtKey:[tag stringKey]];
396 NSLog(@"[EC] filehash=[%@]\n", [tag stringKey]);
397 DownloadingFile *file = [m_downloads objectForKey:[tag stringKey]];
399 file = [DownloadingFile createFromEC:tag];
400 [m_downloads insertObject:file];
402 DownloadingFile *file = [m_downloads objectForKey:[tag stringKey]];
403 [file updateFromEC:tag];
407 NSLog(@"[EC] bad tag type '%s'\n", [t class]);
412 - (void)handleSharedFilesUpdate:(ECPacket *) packet {
413 for (ECTag *t in packet.subtags) {
414 if ( [t isKindOfClass:[ECTagMD5 class]] ) {
415 ECTagMD5 *tag = (ECTagMD5 *)t;
416 if ( [tag tagCount] == 0 ) {
418 // Only hash here - indication to remove the object
420 [m_shared removeAtKey:[tag stringKey]];
422 NSLog(@"[EC] filehash=[%@]\n", [tag stringKey]);
423 SharedFile *file = [m_shared objectForKey:[tag stringKey]];
425 file = [SharedFile createFromEC:tag];
426 [m_shared insertObject:file];
428 SharedFile *file = [m_shared objectForKey:[tag stringKey]];
429 [file updateFromEC:tag];
433 NSLog(@"[EC] bad tag type '%s'\n", [t class]);
438 - (void)handleSearchUpdate:(ECPacket *) packet {
439 for (ECTag *t in packet.subtags) {
440 if ( [t isKindOfClass:[ECTagMD5 class]] ) {
441 ECTagMD5 *tag = (ECTagMD5 *)t;
442 SearchFile *file = [m_search_results objectForKey:[tag stringKey]];
444 file = [SearchFile createFromEC:tag];
445 [m_search_results insertObject:file];
447 SearchFile *file = [m_search_results objectForKey:[tag stringKey]];
448 [file updateFromEC:tag];
454 - (void)handleStatusUpdate:(ECPacket *) packet {
457 - (void)startSearch:(NSString *)text searchType:(EC_SEARCH_TYPE)searchType
458 minSize:(uint64_t)minSize maxSize:(uint64_t)maxSize avail:(uint32_t)avail {
460 ECPacket *packet = [ECPacket packetWithOpcode:EC_OP_SEARCH_START];
462 ECTagInt8 *searchtag = [ECTagInt8 tagFromInt8:searchType withName:EC_TAG_SEARCH_TYPE];
464 [searchtag.subtags addObject:[ECTagString tagFromString:text withName:EC_TAG_SEARCH_NAME]];
466 [searchtag.subtags addObject:[ECTagInt64 tagFromInt64:minSize withName:EC_TAG_SEARCH_MIN_SIZE]];
469 [searchtag.subtags addObject:[ECTagInt64 tagFromInt64:maxSize withName:EC_TAG_SEARCH_MAX_SIZE]];
472 [searchtag.subtags addObject:[ECTagInt32 tagFromInt32:avail withName:EC_TAG_SEARCH_AVAILABILITY]];
475 [packet.subtags addObject:searchtag];
477 [m_connection sendPacket:packet];
479 m_search_running = true;
483 ECPacket *packet = [ECPacket packetWithOpcode:EC_OP_SEARCH_STOP];
485 [m_connection sendPacket:packet];
487 m_search_running = false;