OSDN Git Service

Vss2Git
[peercast-im/PeerCastIM.git] / c: / Git / PeerCast.root / PeerCast / ui / win32 / Simple_vp / gui.cpp
1 // ------------------------------------------------
2 // File : gui.cpp
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //              Windows front end GUI, PeerCast core is not dependant on any of this. 
7 //              Its very messy at the moment, but then again Windows UI always is.
8 //              I really don`t like programming win32 UI.. I want my borland back..
9 //
10 // (c) 2002 peercast.org
11 // ------------------------------------------------
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
16
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 // ------------------------------------------------
22
23 #include <windows.h>
24 #include "stdio.h"
25 #include "string.h"
26 #include "stdarg.h"
27 #include "resource.h"
28 #include "socket.h"
29 #include "win32/wsys.h"
30 #include "servent.h"
31 #include "win32/wsocket.h"
32 #include "inifile.h"
33 #include "gui.h"
34 #include "servmgr.h"
35 #include "peercast.h"
36 #include "simple.h"
37 #ifdef _DEBUG
38 #include "chkMemoryLeak.h"
39 #define DEBUG_NEW new(__FILE__, __LINE__)
40 #define new DEBUG_NEW
41 #endif
42
43 ThreadInfo guiThread;
44 bool shownChannels=false;
45
46 class ListData{
47 public:
48         int channel_id;
49         char name[21];
50         int bitRate;
51         int status;
52         const char *statusStr;
53         int totalListeners;
54         int totalRelays;
55         int localListeners;
56         int localRelays;
57         bool stayConnected;
58         ChanHit chDisp;
59         bool bTracker;
60
61         bool flg;
62         ListData *next;
63 };
64
65 class ServentData{
66 public:
67         int servent_id;
68         unsigned int tnum;
69         int type;
70         int status;
71         String agent;
72         Host h;
73         unsigned int syncpos;
74         char *typeStr;
75         char *statusStr;
76         bool infoFlg;
77         bool relay;
78         bool firewalled;
79         unsigned int numRelays;
80         unsigned int totalRelays;
81         unsigned int totalListeners;
82         int vp_ver;
83         char ver_ex_prefix[2];
84         int ver_ex_number;
85
86         bool flg;
87         ServentData *next;
88
89         unsigned int lastSkipTime;
90         unsigned int lastSkipCount;
91 };
92
93 ListData *list_top = NULL;
94 ServentData *servent_top = NULL;
95
96 // --------------------------------------------------
97 // for PCRaw (connection list) start
98 WNDPROC wndOldListBox = NULL, wndOldConnListBox = NULL;
99 bool sleep_skip = false;
100
101 LRESULT CALLBACK ListBoxProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
102 {
103         switch(message)
104         {
105                 case WM_LBUTTONDOWN:
106                 {
107                         int index = SendMessage(hwnd, LB_ITEMFROMPOINT, 0, lParam);
108                         if(index >= 0x10000)
109                         {
110                                 SendMessage(hwnd, LB_SETCURSEL, (DWORD)-1, 0L);
111                         }
112                         sleep_skip = true;
113                 }
114                         break;
115
116                 case WM_LBUTTONDBLCLK:
117                 {
118                         int index = SendMessage(hwnd, LB_ITEMFROMPOINT, 0, lParam);
119                         if(index < 0x10000)
120                         {
121                                 SendMessage(guiWnd, WM_COMMAND, IDC_BUTTON8, NULL);
122                         }
123                 }
124                         break;
125
126                 case WM_RBUTTONDOWN:
127                 {
128                         POINT pos;
129                         MENUITEMINFO info, separator;
130                         HMENU hMenu;
131                         DWORD dwID;
132
133                         int index = SendMessage(hwnd, LB_ITEMFROMPOINT, 0, lParam);
134                         if(index < 0x10000)
135                         {
136                                 SendMessage(hwnd, LB_SETCURSEL, (DWORD)index, 1L);
137                         }
138                         else
139                         {
140                                 SendMessage(hwnd, LB_SETCURSEL, (DWORD)-1, 0L);
141                                 sleep_skip = true;
142                                 break;
143                         }
144                         
145                         hMenu = CreatePopupMenu();
146
147                         memset(&separator, 0, sizeof(MENUITEMINFO));
148                         separator.cbSize = sizeof(MENUITEMINFO);
149                         separator.fMask = MIIM_ID | MIIM_TYPE;
150                         separator.fType = MFT_SEPARATOR;
151                         separator.wID = 8000;
152
153                         memset(&info, 0, sizeof(MENUITEMINFO));
154                         info.cbSize = sizeof(MENUITEMINFO);
155                         info.fMask = MIIM_ID | MIIM_TYPE;
156                         info.fType = MFT_STRING;
157
158                         info.wID = 1001;
159                         info.dwTypeData = "\90Ø\92f(&X)";
160                         InsertMenuItem(hMenu, -1, true, &info);
161
162                         InsertMenuItem(hMenu, -1, true, &separator);
163
164                         info.wID = 1000;
165                         info.dwTypeData = "\8dÄ\90¶(&P)";
166                         InsertMenuItem(hMenu, -1, true, &info);
167
168                         InsertMenuItem(hMenu, -1, true, &separator);
169
170                         info.wID = 1002;
171                         info.dwTypeData = "\8dÄ\90Ú\91±(&R)";
172                         InsertMenuItem(hMenu, -1, true, &info);
173
174                         info.wID = 1003;
175                         info.dwTypeData = "\83L\81[\83v(&K)";
176                         InsertMenuItem(hMenu, -1, true, &info);
177
178                         InsertMenuItem(hMenu, -1, true, &separator);
179
180                         info.wID = 2000;
181                         info.dwTypeData = "\91I\91ð\89ð\8f\9c(&D)";
182                         InsertMenuItem(hMenu, -1, true, &info);
183
184                         GetCursorPos(&pos);
185                         dwID = TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RETURNCMD, pos.x, pos.y, 0, hwnd, NULL);
186
187                         DestroyMenu(hMenu);
188
189                         switch(dwID)
190                         {
191                         case 1000:
192                                 SendMessage(guiWnd, WM_COMMAND, IDC_BUTTON8, NULL);
193                                 break;
194
195                         case 1001:
196                                 SendMessage(guiWnd, WM_COMMAND, IDC_BUTTON5, NULL);
197                                 break;
198
199                         case 1002:
200                                 SendMessage(guiWnd, WM_COMMAND, IDC_BUTTON3, NULL);
201                                 break;
202
203                         case 1003:
204                                 SendMessage(guiWnd, WM_COMMAND, IDC_BUTTON9, NULL);
205                                 break;
206
207                         case 2000:
208                                 SendMessage(hwnd, LB_SETCURSEL, (DWORD)-1, 0L);
209                                 sleep_skip = true;
210                                 break;
211                         }
212
213                 }
214                         break;
215
216                 case WM_KEYDOWN:
217                         sleep_skip = true;
218                         break;
219         }
220
221         return CallWindowProc(wndOldListBox, hwnd, message, wParam, lParam);
222 }
223
224 LRESULT CALLBACK ConnListBoxProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
225 {
226         switch(message)
227         {
228                 case WM_LBUTTONDOWN:
229                 {
230                         int index = SendMessage(hwnd, LB_ITEMFROMPOINT, 0, lParam);
231                         if(index >= 0x10000)
232                         {
233                                 SendMessage(hwnd, LB_SETCURSEL, (DWORD)-1, 0L);
234                         }
235                 }
236                         break;
237
238                 case WM_RBUTTONDOWN:
239                 {
240                         POINT pos;
241                         MENUITEMINFO info;
242                         HMENU hMenu;
243                         DWORD dwID;
244
245                         int index = SendMessage(hwnd, LB_ITEMFROMPOINT, 0, lParam);
246                         if(index < 0x10000)
247                         {
248                                 SendMessage(hwnd, LB_SETCURSEL, (DWORD)index, 1L);
249                         }
250                         else
251                         {
252                                 SendMessage(hwnd, LB_SETCURSEL, (DWORD)-1, 0L);
253                                 break;
254                         }
255                         
256                         hMenu = CreatePopupMenu();
257
258                         memset(&info, 0, sizeof(MENUITEMINFO));
259                         info.cbSize = sizeof(MENUITEMINFO);
260                         info.fMask = MIIM_ID | MIIM_TYPE;
261                         info.fType = MFT_STRING;
262
263                         info.wID = 1001;
264                         info.dwTypeData = "\90Ø\92f(&X)";
265                         InsertMenuItem(hMenu, -1, true, &info);
266
267                         GetCursorPos(&pos);
268                         dwID = TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RETURNCMD, pos.x, pos.y, 0, hwnd, NULL);
269
270                         DestroyMenu(hMenu);
271
272                         switch(dwID)
273                         {
274                         case 1001:
275                                 SendMessage(guiWnd, WM_COMMAND, IDC_BUTTON6, NULL);
276                                 break;
277                         }
278
279                 }
280                         break;
281         }
282
283         return CallWindowProc(wndOldConnListBox, hwnd, message, wParam, lParam);
284 }
285 // for PCRaw (connection list) end
286 // --------------------------------------------------
287 int logID,statusID,hitID,chanID;
288
289 // --------------------------------------------------
290 bool getButtonState(int id)
291 {
292         return SendDlgItemMessage(guiWnd, id,BM_GETCHECK, 0, 0) == BST_CHECKED;
293 }
294
295 // --------------------------------------------------
296 void enableControl(int id, bool on)
297 {
298         EnableWindow(GetDlgItem(guiWnd,id),on);
299 }
300
301 // --------------------------------------------------
302 void enableEdit(int id, bool on)
303 {
304         SendDlgItemMessage(guiWnd, id,WM_ENABLE, on, 0);
305         SendDlgItemMessage(guiWnd, id,EM_SETREADONLY, !on, 0);
306 }
307 // --------------------------------------------------
308 int getEditInt(int id)
309 {
310         char str[128];
311         SendDlgItemMessage(guiWnd, id,WM_GETTEXT, 128, (LONG)str);
312         return atoi(str);
313 }
314 // --------------------------------------------------
315 char * getEditStr(int id)
316 {
317         static char str[128];
318         SendDlgItemMessage(guiWnd, id,WM_GETTEXT, 128, (LONG)str);
319         return str;
320 }
321 // --------------------------------------------------
322 void setEditStr(int id, char *str)
323 {
324         SendDlgItemMessage(guiWnd, id,WM_SETTEXT, 0, (LONG)str);
325 }
326 // --------------------------------------------------
327 void setEditInt(int id, int v)
328 {
329         char str[128];
330         sprintf(str,"%d",v);
331         SendDlgItemMessage(guiWnd, id,WM_SETTEXT, 0, (LONG)str);
332 }
333
334 // --------------------------------------------------
335 void *getListBoxSelData(int id)
336 {
337         int sel = SendDlgItemMessage(guiWnd, id,LB_GETCURSEL, 0, 0);
338         if (sel >= 0)
339                 return (void *)SendDlgItemMessage(guiWnd, id,LB_GETITEMDATA, sel, 0);
340         return NULL;
341 }
342
343 Channel *getListBoxChannel(){
344         int sel = SendDlgItemMessage(guiWnd, chanID ,LB_GETCURSEL, 0, 0);
345         if (sel >= 0){
346                 ListData *ld = list_top;
347                 int idx = 0;
348
349                 while(ld){
350                         if (sel == idx){
351                                 return chanMgr->findChannelByChannelID(ld->channel_id);
352                         }
353                         ld = ld->next;
354                         idx++;
355                 }
356         }
357         return NULL;
358 }
359
360 Servent *getListBoxServent(){
361         int sel = SendDlgItemMessage(guiWnd, statusID ,LB_GETCURSEL, 0, 0);
362         if (sel >= 0){
363                 ServentData *sd = servent_top;
364                 int idx = 0;
365
366                 while(sd){
367                         if (sel == idx){
368                                 return servMgr->findServentByServentID(sd->servent_id);
369                         }
370                         sd = sd->next;
371                         idx++;
372                 }
373         }
374         return NULL;
375 }
376
377 // --------------------------------------------------
378 void setButtonState(int id, bool on)
379 {
380         SendDlgItemMessage(guiWnd, id,BM_SETCHECK, on, 0);
381         SendMessage(guiWnd,WM_COMMAND,id,0);
382 }
383 // --------------------------------------------------
384 void ADDLOG(const char *str,int id,bool sel,void *data, LogBuffer::TYPE type)
385 {
386         if (guiWnd)
387         {
388
389                 String sjis; //JP-EX
390                 int num = SendDlgItemMessage(guiWnd, id,LB_GETCOUNT, 0, 0);
391                 if (num > 100)
392                 {
393                         SendDlgItemMessage(guiWnd, id, LB_DELETESTRING, 0, 0);
394                         num--;
395                 }
396                 sjis = str; //JP-Patch
397                 sjis.convertTo(String::T_SJIS); //JP-Patch
398                 //int idx = SendDlgItemMessage(guiWnd, id, LB_ADDSTRING, 0, (LONG)(LPSTR)str);
399                 int idx = SendDlgItemMessage(guiWnd, id, LB_ADDSTRING, 0, (LONG)(LPSTR)sjis.cstr());
400                 SendDlgItemMessage(guiWnd, id, LB_SETITEMDATA, idx, (LONG)data);
401
402                 if (sel)
403                         SendDlgItemMessage(guiWnd, id, LB_SETCURSEL, num, 0);
404
405         }
406
407         if (type != LogBuffer::T_NONE)
408         {
409 #if _DEBUG
410                 OutputDebugString(str);
411                 OutputDebugString("\n");
412 #endif
413         }
414
415 }
416
417
418 // --------------------------------------------------
419 void ADDLOG2(const char *fmt,va_list ap,int id,bool sel,void *data, LogBuffer::TYPE type)
420 {
421         char str[4096];
422         vsprintf(str,fmt,ap);
423
424         ADDLOG(str,id,sel,data,type);
425 }
426
427 // --------------------------------------------------
428 void ADDCHAN(void *d, const char *fmt,...)
429 {
430         va_list ap;
431         va_start(ap, fmt);
432         ADDLOG2(fmt,ap,chanID,false,d,LogBuffer::T_NONE);
433         va_end(ap);     
434 }
435 // --------------------------------------------------
436 void ADDHIT(void *d, const char *fmt,...)
437 {
438         va_list ap;
439         va_start(ap, fmt);
440         ADDLOG2(fmt,ap,hitID,false,d,LogBuffer::T_NONE);
441         va_end(ap);     
442 }
443 // --------------------------------------------------
444 void ADDCONN(void *d, const char *fmt,...)
445 {
446         va_list ap;
447         va_start(ap, fmt);
448         ADDLOG2(fmt,ap,statusID,false,d,LogBuffer::T_NONE);
449         va_end(ap);     
450 }
451
452 WLock sd_lock;
453 WLock ld_lock;
454
455 // --------------------------------------------------
456 THREAD_PROC showConnections(ThreadInfo *thread)
457 {
458 //      thread->lock();
459         while (thread->active)
460         {
461                 int sel,top,i;
462 /*              sel = SendDlgItemMessage(guiWnd, statusID,LB_GETCURSEL, 0, 0);
463                 top = SendDlgItemMessage(guiWnd, statusID,LB_GETTOPINDEX, 0, 0);
464
465                 SendDlgItemMessage(guiWnd, statusID, LB_RESETCONTENT, 0, 0);
466                 Servent *s = servMgr->servents;
467                 while (s)
468                 {
469                         if (s->type != Servent::T_NONE)
470                         {
471                                 Host h = s->getHost();
472                                 {
473                                         unsigned int ip = h.ip;
474                                         unsigned short port = h.port;
475
476                                         Host h(ip,port);
477                                         char hostName[64];
478                                         h.toStr(hostName);
479
480                                         unsigned int tnum = 0;
481                                         char tdef = 's';
482                                         if (s->lastConnect)
483                                                 tnum = sys->getTime()-s->lastConnect;
484
485                                         if ((s->type == Servent::T_RELAY) || (s->type == Servent::T_DIRECT))
486                                         {
487                                                 ADDCONN(s,"%s-%s-%d%c  -  %s  -  %d - %s ",
488                                                         s->getTypeStr(),s->getStatusStr(),tnum,tdef,
489                                                         hostName,
490                                                         s->syncPos,s->agent.cstr()
491                                                         ); //JP-Patch
492                                         }else{
493                                                 if (s->status == Servent::S_CONNECTED)
494                                                 {
495                                                         ADDCONN(s,"%s-%s-%d%c  -  %s  -  %d/%d",
496                                                                 s->getTypeStr(),s->getStatusStr(),tnum,tdef,
497                                                                 hostName,
498                                                                 s->gnuStream.packetsIn,s->gnuStream.packetsOut);
499                                                 }else{
500                                                         ADDCONN(s,"%s-%s-%d%c  -  %s",
501                                                                 s->getTypeStr(),s->getStatusStr(),tnum,tdef,
502                                                                 hostName
503                                                                 );
504                                                 }
505
506                                         }
507
508                                 }
509                         }
510                         s=s->next;
511                 }
512                 if (sel >= 0)
513                         SendDlgItemMessage(guiWnd, statusID,LB_SETCURSEL, sel, 0);
514                 if (top >= 0)
515                         SendDlgItemMessage(guiWnd, statusID,LB_SETTOPINDEX, top, 0);*/
516
517                 int sd_count = 0;
518                 int diff = 0;
519
520                 sel = SendDlgItemMessage(guiWnd, statusID,LB_GETCURSEL, 0, 0);
521                 top = SendDlgItemMessage(guiWnd, statusID,LB_GETTOPINDEX, 0, 0);
522
523                 ServentData *sd = servent_top;
524                 while(sd){
525                         sd->flg = false;
526                         sd = sd->next;
527                         sd_count++;
528                 }
529
530                 servMgr->lock.on();
531                 Servent *s = servMgr->servents;
532
533                 Channel *sel_ch = getListBoxChannel();          // for PCRaw (connection list)
534
535                 while(s){
536                         Servent *next;
537                         bool foundFlg = false;
538                         ChanHitList *chl;
539                         bool infoFlg = false;
540                         bool relay = true;
541                         bool firewalled = false;
542                         unsigned int numRelays = 0;
543                         unsigned int totalRelays = 0;
544                         unsigned int totalListeners = 0;
545                         int vp_ver = 0;
546                         char ver_ex_prefix[2] = {' ', ' '};
547                         int ver_ex_number = 0;
548
549                         next = s->next;
550
551                         // for PCRaw (connection list) start
552                         if(sel_ch && !sel_ch->info.id.isSame(s->chanID))
553                         {
554                                 s = next;
555                                 continue;
556                         }
557                         // for PCRaw (connection list) end
558
559                         if (s->type != Servent::T_NONE){
560
561                                 chanMgr->hitlistlock.on();
562                                 
563                                 chl = chanMgr->findHitListByID(s->chanID);
564                                 if (chl){
565                                         ChanHit *hit = chl->hit;
566                                         while(hit){
567                                                 if (hit->servent_id == s->servent_id){
568                                                         if ((hit->numHops == 1)/* && (hit->host.ip == s->getHost().ip)*/){
569                                                                 infoFlg = true;
570                                                                 relay = hit->relay;
571                                                                 firewalled = hit->firewalled;
572                                                                 numRelays = hit->numRelays;
573                                                                 vp_ver = hit->version_vp;
574                                                                 ver_ex_prefix[0] = hit->version_ex_prefix[0];
575                                                                 ver_ex_prefix[1] = hit->version_ex_prefix[1];
576                                                                 ver_ex_number = hit->version_ex_number;
577                                                         }
578                                                         totalRelays += hit->numRelays;
579                                                         totalListeners += hit->numListeners;
580                                                 }
581                                                 hit = hit->next;
582                                         }
583                                 }
584                                 chanMgr->hitlistlock.off();
585                         }
586
587                         sd = servent_top;
588                         while(sd){
589                                 if (sd->servent_id == s->servent_id){
590                                         foundFlg = true;
591                                         if (s->thread.finish) break;
592                                         sd->flg = true;
593                                         sd->type = s->type;
594                                         sd->status = s->status;
595                                         sd->agent = s->agent;
596                                         sd->h = s->getHost();
597                                         sd->syncpos = s->syncPos;
598                                         sd->tnum = (s->lastConnect) ? sys->getTime()-s->lastConnect : 0;
599                                         sd->typeStr = s->getTypeStr();
600                                         sd->statusStr = s->getStatusStr();
601                                         sd->infoFlg = infoFlg;
602                                         sd->relay = relay;
603                                         sd->firewalled = firewalled;
604                                         sd->numRelays = numRelays;
605                                         sd->totalRelays = totalRelays;
606                                         sd->totalListeners = totalListeners;
607                                         sd->vp_ver = vp_ver;
608                                         sd->lastSkipTime = s->lastSkipTime;
609                                         sd->lastSkipCount = s->lastSkipCount;
610                                         sd->ver_ex_prefix[0] = ver_ex_prefix[0];
611                                         sd->ver_ex_prefix[1] = ver_ex_prefix[1];
612                                         sd->ver_ex_number = ver_ex_number;
613                                         break;
614                                 }
615                                 sd = sd->next;
616                         }
617                         if (!foundFlg && (s->type != Servent::T_NONE) && !s->thread.finish){
618                                 ServentData *newData = new ServentData();
619                                 newData->next = servent_top;
620                                 servent_top = newData;
621                                 newData->flg = true;
622                                 newData->servent_id = s->servent_id;
623                                 newData->type = s->type;
624                                 newData->status = s->status;
625                                 newData->agent = s->agent;
626                                 newData->h = s->getHost();
627                                 newData->syncpos = s->syncPos;
628                                 newData->tnum = (s->lastConnect) ? sys->getTime()-s->lastConnect : 0;
629                                 newData->typeStr = s->getTypeStr();
630                                 newData->statusStr = s->getStatusStr();
631                                 newData->infoFlg = infoFlg;
632                                 newData->relay = relay;
633                                 newData->firewalled = firewalled;
634                                 newData->numRelays = numRelays;
635                                 newData->totalRelays = totalRelays;
636                                 newData->totalListeners = totalListeners;
637                                 newData->vp_ver = vp_ver;
638                                 newData->lastSkipTime = s->lastSkipTime;
639                                 newData->lastSkipCount = s->lastSkipCount;
640                                 newData->ver_ex_prefix[0] = ver_ex_prefix[0];
641                                 newData->ver_ex_prefix[1] = ver_ex_prefix[1];
642                                 newData->ver_ex_number = ver_ex_number;
643
644                                 int idx = SendDlgItemMessage(guiWnd, statusID, LB_ADDSTRING, 0, (LONG)"");
645                                 SendDlgItemMessage(guiWnd, statusID, LB_SETITEMDATA, idx, (LONG)(newData->servent_id));
646                                 diff++;
647                         }
648                         s = next;
649                 }
650                 servMgr->lock.off();
651
652                 sd_lock.on();
653                 sd = servent_top;
654                 int idx = 0;
655                 ServentData *prev = NULL;
656                 //int *idxs;
657                 //if (sd_count){
658                 //      idxs = new int[sd_count];
659                 //}
660                 while(sd){
661                         if (!sd->flg || (sd->type == Servent::T_NONE)){
662                                 ServentData *next = sd->next;
663                                 if (!prev){
664                                         servent_top = next;
665                                 } else {
666                                         prev->next = next;
667                                 }
668                                 delete sd;
669
670                                 PostMessage(GetDlgItem(guiWnd, statusID), LB_DELETESTRING, idx, 0);
671 //                              SendDlgItemMessage(guiWnd, statusID, LB_DELETESTRING, idx, 0);
672                                 sd = next;
673 //                              diff--;
674                         } else {
675                                 idx++;
676                                 prev = sd;
677                                 sd = sd->next;
678                         }
679                 }
680                 sd_lock.off();
681
682                 if ((sel >= 0) && (diff != 0)){
683                         PostMessage(GetDlgItem(guiWnd, statusID), LB_SETCURSEL, sel+diff, 0);
684                 }
685                 if (top >= 0){
686                         PostMessage(GetDlgItem(guiWnd, statusID), LB_SETTOPINDEX, top, 0);
687                 }
688                 InvalidateRect(GetDlgItem(guiWnd, statusID), NULL, FALSE);
689
690                 char cname[34];
691
692                 {
693 //                      sel = SendDlgItemMessage(guiWnd, chanID,LB_GETCURSEL, 0, 0);
694 //                      top = SendDlgItemMessage(guiWnd, chanID,LB_GETTOPINDEX, 0, 0);
695 //                      SendDlgItemMessage(guiWnd, chanID, LB_RESETCONTENT, 0, 0);
696
697                         ListData *ld = list_top;
698                         while(ld){
699                                 ld->flg = false;
700                                 ld = ld->next;
701                         }
702
703                         Channel *c = chanMgr->channel;
704
705                         while (c)
706                         {
707                                 Channel *next;
708                                 bool foundFlg = false;
709                                 String sjis;
710                                 sjis = c->getName();
711                                 sjis.convertTo(String::T_SJIS);
712                 
713                                 next = c->next;
714
715                                 ld = list_top;
716                                 while(ld){
717                                         if (ld->channel_id == c->channel_id){
718                                                 foundFlg = true;
719                                                 if (c->thread.finish) break;
720                                                 ld->flg = true;
721                                                 strncpy(ld->name, sjis, 20);
722                                                 ld->name[20] = '\0';
723                                                 ld->bitRate = c->info.bitrate;
724                                                 ld->status = c->status;
725                                                 ld->statusStr = c->getStatusStr();
726                                                 ld->totalListeners = c->totalListeners();
727                                                 ld->totalRelays = c->totalRelays();
728                                                 ld->localListeners = c->localListeners();
729                                                 ld->localRelays = c->localRelays();
730                                                 ld->stayConnected = c->stayConnected;
731                                                 ld->chDisp = c->chDisp;
732                                                 ld->bTracker = c->sourceHost.tracker;
733                                                 break;
734                                         }
735                                         ld = ld->next;
736                                 }
737                                 if (!foundFlg && !c->thread.finish){
738                                         ListData *newData = new ListData();
739                                         newData->next = list_top;
740                                         list_top = newData;
741                                         newData->flg = true;
742                                         newData->channel_id = c->channel_id;
743                                         strncpy(newData->name, sjis, 20);
744                                         newData->name[20] = '\0';
745                                         newData->bitRate = c->info.bitrate;
746                                         newData->status = c->status;
747                                         newData->statusStr = c->getStatusStr();
748                                         newData->totalListeners = c->totalListeners();
749                                         newData->totalRelays = c->totalRelays();
750                                         newData->localListeners = c->localListeners();
751                                         newData->localRelays = c->localRelays();
752                                         newData->stayConnected = c->stayConnected;
753                                         newData->chDisp = c->chDisp;
754                                         newData->bTracker = c->sourceHost.tracker;
755
756                                         int idx = SendDlgItemMessage(guiWnd, chanID, LB_ADDSTRING, 0, (LONG)"");
757                                         SendDlgItemMessage(guiWnd, chanID, LB_SETITEMDATA, idx, (LONG)(newData->channel_id));
758                                 }
759                                 c = next;
760                         }
761
762                         ld = list_top;
763                         int idx = 0;
764                         ListData *prev = NULL;
765                         while(ld){
766                                 if (!ld->flg){
767                                         ListData *next = ld->next;
768                                         if (!prev){
769                                                 list_top = next;
770                                         } else {
771                                                 prev->next = next;
772                                         }
773                                         delete ld;
774
775                                         SendDlgItemMessage(guiWnd, chanID, LB_DELETESTRING, idx, 0);
776                                         ld = next;
777                                 } else {
778                                         idx++;
779                                         prev = ld;
780                                         ld = ld->next;
781                                 }
782                         }
783
784                         InvalidateRect(GetDlgItem(guiWnd, chanID), NULL, FALSE);
785
786 /*                                      String sjis; //JP-Patch
787                                         sjis = c->getName(); //JP-Patch
788                                         sjis.convertTo(String::T_SJIS); //JP-Patch
789                                         strncpy(cname,sjis.cstr(),16); //JP-Patch
790                                         //strncpy(cname,c->getName(),16);
791                                         cname[16] = 0;
792                                         //int sec = ((c->currSPacket*c->bitrate*SPacket::DATA_LEN)/8)/(c->bitrate*1024);
793                                         //int k = ((c->currSPacket*SPacket::DATA_LEN))/(1024);
794                                         //ADDCHAN(c,"%d. %s - %d KB/s - %du - %dk",num,cname,c->bitrate,c->listeners,k);
795                                         //ADDCHAN(c,"%d. %s - %d kb/s - %s",c->index,cname,c->getBitrate(),c->getStatusStr());
796                                         ADDCHAN(c,"%s - %d kb/s - %s - %d/%d-[%d/%d] - %s",cname,c->getBitrate(),c->getStatusStr(),
797                                                         c->totalListeners(),c->totalRelays(),c->localListeners(),c->localRelays(),c->stayConnected?"Yes":"No"); //JP-Patch
798                                 }
799                                 c=c->next;
800                         }*/
801 //                      if (sel >= 0)
802 //                              SendDlgItemMessage(guiWnd, chanID,LB_SETCURSEL, sel, 0);
803 //                      if (top >= 0)
804 //                              SendDlgItemMessage(guiWnd, chanID,LB_SETTOPINDEX, top, 0);
805                 }
806
807
808
809                 bool update = ((sys->getTime() - chanMgr->lastHit) < 3)||(!shownChannels);
810
811                 if (update)
812                 {
813                         shownChannels = true;
814                         {
815                                 sel = SendDlgItemMessage(guiWnd, hitID,LB_GETCURSEL, 0, 0);
816                                 top = SendDlgItemMessage(guiWnd, hitID,LB_GETTOPINDEX, 0, 0);
817                                 SendDlgItemMessage(guiWnd, hitID, LB_RESETCONTENT, 0, 0);
818
819                                 chanMgr->hitlistlock.on();
820
821                                 ChanHitList *chl = chanMgr->hitlist;
822
823                                 while (chl)
824                                 {
825                                         if (chl->isUsed())
826                                         {
827                                                 if (chl->info.match(chanMgr->searchInfo))
828                                                 {
829                                                         strncpy(cname,chl->info.name.cstr(),16);
830                                                         cname[16] = 0;
831                                                         ADDHIT(chl,"%s - %d kb/s - %d/%d",cname,chl->info.bitrate,chl->numListeners(),chl->numHits());
832                                                 }
833                                         }
834                                         chl = chl->next;
835                                 }
836                                 chanMgr->hitlistlock.off();
837                         }
838
839                         if (sel >= 0)
840                                 SendDlgItemMessage(guiWnd, hitID,LB_SETCURSEL, sel, 0);
841                         if (top >= 0)
842                                 SendDlgItemMessage(guiWnd, hitID,LB_SETTOPINDEX, top, 0);
843                 }
844
845
846
847
848                 {
849                         switch (servMgr->getFirewall())
850                         {
851                                 case ServMgr::FW_ON:
852                                         SendDlgItemMessage(guiWnd, IDC_EDIT4,WM_SETTEXT, 0, (LONG)"Firewalled");
853                                         break;
854                                 case ServMgr::FW_UNKNOWN:
855                                         SendDlgItemMessage(guiWnd, IDC_EDIT4,WM_SETTEXT, 0, (LONG)"Unknown");
856                                         break;
857                                 case ServMgr::FW_OFF:
858                                         SendDlgItemMessage(guiWnd, IDC_EDIT4,WM_SETTEXT, 0, (LONG)"Normal");
859                                         break;
860                         }
861                 }
862
863                 // sleep for 1 second .. check every 1/10th for shutdown
864                 for(i=0; i<10; i++)
865                 {
866                         if(sleep_skip)  // for PCRaw (connection list)
867                         {
868                                 sleep_skip = false;
869                                 break;
870                         }
871
872                         if (!thread->active)
873                                 break;
874                         sys->sleep(100);
875                 }
876         }
877
878         ListData *ld = list_top;
879         while(ld){
880                 ListData *next;
881                 next = ld->next;
882
883                 delete ld;
884
885                 ld = next;
886         }
887         list_top = NULL;
888
889         ServentData *sd = servent_top;
890         while(sd){
891                 ServentData *next;
892                 next = sd->next;
893
894                 delete sd;
895
896                 sd = next;
897         }
898         servent_top = NULL;
899
900 //      thread->unlock();
901         return 0;
902 }
903
904
905 // --------------------------------------------------
906 void tryConnect()
907 {
908 #if 0
909         ClientSocket sock;
910
911         char tmp[32];
912
913         char *sendStr = "GET / HTTP/1.1\n\n";
914
915         try {
916                 sock.open("taiyo",80);
917                 sock.write(sendStr,strlen(sendStr));
918                 sock.read(tmp,32);
919                 LOG("Connected: %s",tmp);
920         }catch(IOException &e)
921         {
922                 LOG(e.msg);
923         }
924 #endif
925 }
926
927
928 // ---------------------------------
929 void APICALL MyPeercastApp ::printLog(LogBuffer::TYPE t, const char *str)
930 {
931         ADDLOG(str,logID,true,NULL,t);
932         if (logFile.isOpen())
933         {
934                 logFile.writeLine(str);
935                 logFile.flush();
936         }
937 }
938
939
940 // --------------------------------------------------
941 static void setControls(bool fromGUI)
942 {
943         if (!guiWnd)
944                 return;
945         setEditInt(IDC_EDIT1,servMgr->serverHost.port);
946         setEditStr(IDC_EDIT3,servMgr->password);
947         setEditStr(IDC_EDIT9,chanMgr->broadcastMsg.cstr());
948         setEditInt(IDC_MAXRELAYS,servMgr->maxRelays);
949
950         setButtonState(IDC_CHECK11,chanMgr->broadcastMsg[0]!=0);
951
952         setButtonState(IDC_LOGDEBUG,(servMgr->showLog&(1<<LogBuffer::T_DEBUG))!=0);
953         setButtonState(IDC_LOGERRORS,(servMgr->showLog&(1<<LogBuffer::T_ERROR))!=0);
954         setButtonState(IDC_LOGNETWORK,(servMgr->showLog&(1<<LogBuffer::T_NETWORK))!=0);
955         setButtonState(IDC_LOGCHANNELS,(servMgr->showLog&(1<<LogBuffer::T_CHANNEL))!=0);
956
957         setButtonState(IDC_CHECK9,servMgr->pauseLog);
958
959
960         if (!fromGUI)
961                 setButtonState(IDC_CHECK1,servMgr->autoServe);
962
963
964 }
965 // --------------------------------------------------
966 void APICALL MyPeercastApp::updateSettings()
967 {
968         setControls(true);
969 }
970
971 void MoveControl(HWND hWnd, int cx, int cy, int y, HDWP& hDwp){
972         RECT rc2;
973         POINT pos;
974
975         GetWindowRect(hWnd, &rc2);
976         pos.x = rc2.left;
977         pos.y = rc2.top;
978         ScreenToClient(guiWnd, &pos);
979         hDwp = DeferWindowPos(hDwp, hWnd, HWND_TOP,
980                 pos.x,cy-y-(rc2.bottom-rc2.top),rc2.right-rc2.left,rc2.bottom-rc2.top,SWP_SHOWWINDOW);
981 }
982
983 void MoveControl2(HWND hWnd, int cx, int cy, int y, HDWP& hDwp){
984         RECT rc;
985         POINT pos;
986         
987         GetWindowRect(hWnd, &rc);
988         pos.x = rc.left;
989         pos.y = rc.top;
990         ScreenToClient(guiWnd, &pos);
991         hDwp = DeferWindowPos(hDwp, hWnd, HWND_TOP,
992                 pos.x,pos.y,rc.right-rc.left,cy-y,SWP_SHOWWINDOW);
993 }
994
995 // --------------------------------------------------
996 void MoveControls(LPARAM lParam){
997     HDWP hDwp;
998                         //IDC_LIST1 3,291,291,43
999 /*        CONTROL         "\97L\8cø",IDC_CHECK1,"Button",BS_AUTOCHECKBOX | BS_PUSHLIKE | 
1000                     WS_TABSTOP,9,29,60,20,WS_EX_TRANSPARENT
1001     EDITTEXT        IDC_EDIT1,127,18,47,12,ES_AUTOHSCROLL
1002     RTEXT           "\83|\81[\83g :",IDC_STATIC,107,20,18,8
1003     GROUPBOX        "",IDC_STATIC,3,4,291,49
1004     PUSHBUTTON      "\90Ø\92f",IDC_BUTTON5,67,65,43,13
1005     EDITTEXT        IDC_EDIT3,127,34,47,12,ES_PASSWORD | ES_AUTOHSCROLL
1006     RTEXT           "\83p\83X\83\8f\81[\83h :",IDC_STATIC,89,36,36,8
1007     PUSHBUTTON      "\8dÄ\90¶",IDC_BUTTON8,10,65,22,13
1008     PUSHBUTTON      "\8dÄ\90Ú\91±",IDC_BUTTON3,41,65,24,13
1009     RTEXT           "\8dÅ\91å\83\8a\83\8c\81[\90\94 :",IDC_STATIC,203,20,40,8
1010     EDITTEXT        IDC_MAXRELAYS,248,18,40,14,ES_AUTOHSCROLL | ES_NUMBER
1011     PUSHBUTTON      "\83L\81[\83v",IDC_BUTTON9,112,65,24,13
1012     LTEXT           "Peercast-VP",IDC_STATIC,21,14,39,8*/
1013
1014 /*
1015     GROUPBOX        "\83\8a\83\8c\81[",IDC_GROUPBOX_RELAY,3,54,291,132
1016     LISTBOX         IDC_LIST3,3,81,291,102,LBS_OWNERDRAWFIXED | 
1017                     LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
1018     CONTROL         "DJ",IDC_CHECK11,"Button",BS_AUTOCHECKBOX | BS_PUSHLIKE | 
1019                     WS_TABSTOP,5,190,23,12
1020     EDITTEXT        IDC_EDIT9,33,189,261,14,ES_AUTOHSCROLL
1021     LTEXT           "\83R\83l\83N\83V\83\87\83\93",IDC_STATIC_CONNECTION,3,214,40,8
1022     PUSHBUTTON      "\90Ø\92f",IDC_BUTTON6,47,209,43,13
1023     LISTBOX         IDC_LIST2,3,224,291,53,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | 
1024                     WS_TABSTOP
1025     LTEXT           "\83\8d\83O",IDC_STATIC_LOG,3,282,13,8
1026     PUSHBUTTON      "\83N\83\8a\83A",IDC_BUTTON1,35,279,25,11
1027     CONTROL         "\92â\8e~",IDC_CHECK9,"Button",BS_AUTOCHECKBOX | BS_PUSHLIKE | 
1028                     WS_TABSTOP,60,279,30,11
1029     CONTROL         "\83f\83o\83b\83O",IDC_LOGDEBUG,"Button",BS_AUTOCHECKBOX | 
1030                     BS_PUSHLIKE | WS_TABSTOP,127,279,32,11
1031     CONTROL         "\83G\83\89\81[",IDC_LOGERRORS,"Button",BS_AUTOCHECKBOX | 
1032                     BS_PUSHLIKE | WS_TABSTOP,159,279,25,11
1033     CONTROL         "\83l\83b\83g\83\8f\81[\83N",IDC_LOGNETWORK,"Button",BS_AUTOCHECKBOX | 
1034                     BS_PUSHLIKE | WS_TABSTOP,185,279,35,11
1035     CONTROL         "\83`\83\83\83\93\83l\83\8b",IDC_LOGCHANNELS,"Button",BS_AUTOCHECKBOX | 
1036                     BS_PUSHLIKE | WS_TABSTOP,221,279,35,11
1037         LISTBOX         IDC_LIST1,3,291,291,43,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | 
1038                     WS_TABSTOP*/
1039         
1040         hDwp = BeginDeferWindowPos(15);
1041         int cx = LOWORD(lParam);
1042         int cy = HIWORD(lParam);
1043         
1044         MoveControl2(GetDlgItem(guiWnd, IDC_GROUPBOX_RELAY), cx, cy, 343, hDwp);
1045         MoveControl2(GetDlgItem(guiWnd, IDC_LIST3), cx, cy, 385, hDwp);
1046         
1047         MoveControl(GetDlgItem(guiWnd, IDC_EDIT9), cx, cy, 233, hDwp);
1048         MoveControl(GetDlgItem(guiWnd, IDC_CHECK11), cx, cy, 232, hDwp);
1049         MoveControl(GetDlgItem(guiWnd, IDC_STATIC_CONNECTION), cx, cy, 209, hDwp);
1050         MoveControl(GetDlgItem(guiWnd, IDC_BUTTON6), cx, cy, 208, hDwp);
1051         MoveControl(GetDlgItem(guiWnd, IDC_LIST2), cx, cy, 94, hDwp);
1052         MoveControl(GetDlgItem(guiWnd, IDC_STATIC_LOG), cx, cy, 72, hDwp);
1053         MoveControl(GetDlgItem(guiWnd, IDC_CHECK9), cx, cy, 71, hDwp);
1054         MoveControl(GetDlgItem(guiWnd, IDC_BUTTON1), cx, cy, 71, hDwp);
1055         MoveControl(GetDlgItem(guiWnd, IDC_LOGDEBUG), cx, cy, 71, hDwp);
1056         MoveControl(GetDlgItem(guiWnd, IDC_LOGNETWORK), cx, cy, 71, hDwp);
1057         MoveControl(GetDlgItem(guiWnd, IDC_LOGERRORS), cx, cy, 71, hDwp);
1058         MoveControl(GetDlgItem(guiWnd, IDC_LOGCHANNELS), cx, cy, 71, hDwp);
1059         MoveControl(GetDlgItem(guiWnd, IDC_LIST1), cx, cy, 3, hDwp);
1060
1061         EndDeferWindowPos(hDwp);
1062 }
1063
1064 WINDOWPLACEMENT winPlace;
1065 bool guiFlg = false;
1066
1067 // --------------------------------------------------
1068 LRESULT CALLBACK GUIProc (HWND hwnd, UINT message,
1069                                  WPARAM wParam, LPARAM lParam)
1070 {
1071
1072         static const struct 
1073                 {
1074                         LRESULT from; // \95Ï\8a·\8c³
1075                         LRESULT to;   // \95Ï\8a·\90æ
1076                 }
1077         trans[] = 
1078                 {
1079                         { HTLEFT,        HTBORDER }, // \8d\92[\82Ì\8d\89E\96î\88ó \81¨ \96î\88ó\82È\82µ
1080                         { HTRIGHT,       HTBORDER }, // \89E\92[\82Ì\8d\89E\96î\88ó \81¨ \96î\88ó\82È\82µ
1081                         { HTTOPLEFT,     HTTOP    }, // \8d\8fã\8b÷\82Ì\8eÎ\82ß\96î\88ó \81¨ \8fc\96î\88ó
1082                         { HTTOPRIGHT,    HTTOP    }, // \89E\8fã\8b÷\82Ì\8eÎ\82ß\96î\88ó \81¨ \8fc\96î\88ó
1083                         { HTBOTTOMLEFT,  HTBOTTOM }, // \8d\89º\8b÷\82Ì\8eÎ\82ß\96î\88ó \81¨ \8fc\96î\88ó
1084                         { HTBOTTOMRIGHT, HTBOTTOM }, // \89E\89º\8b÷\82Ì\8eÎ\82ß\96î\88ó \81¨ \8fc\96î\88ó
1085                         { HTTOP,         HTTOP    },
1086                         { HTBOTTOM,      HTBOTTOM }
1087                 };
1088
1089
1090         bool rectflg = false;
1091
1092    switch (message)
1093    {
1094        case WM_INITDIALOG:
1095                         guiWnd = hwnd;
1096
1097                         shownChannels = false;
1098                         logID = IDC_LIST1;              // log
1099                         statusID = IDC_LIST2;   // status
1100                         hitID = IDC_LIST4;              // hit
1101                         chanID = IDC_LIST3;             // channels
1102
1103                         enableControl(IDC_BUTTON8,false);
1104                         enableControl(IDC_BUTTON11,false);
1105                         enableControl(IDC_BUTTON10,false);
1106                         
1107                         peercastApp->updateSettings();
1108
1109                         if (servMgr->autoServe)
1110                                 setButtonState(IDC_CHECK1,true);
1111                         if (servMgr->autoConnect)
1112                                 setButtonState(IDC_CHECK2,true);
1113
1114
1115                         guiThread.func = showConnections;
1116                         if (!sys->startThread(&guiThread))
1117                         {
1118                                 MessageBox(hwnd,"Unable to start GUI","PeerCast",MB_OK|MB_ICONERROR);
1119                                 PostMessage(hwnd,WM_DESTROY,0,0);
1120                         }
1121
1122                         if (guiFlg){
1123                                 SetWindowPlacement(hwnd, &winPlace);
1124                         }
1125
1126                         {       // for PCRaw (connection list)
1127                                 HWND hwndList;
1128                                 
1129                                 hwndList = GetDlgItem(guiWnd, chanID);
1130                                 wndOldListBox = (WNDPROC)GetWindowLong(hwndList, GWL_WNDPROC);
1131                                 SetWindowLong(hwndList, GWL_WNDPROC, (DWORD)ListBoxProc);
1132
1133                                 hwndList = GetDlgItem(guiWnd, statusID);
1134                                 wndOldConnListBox = (WNDPROC)GetWindowLong(hwndList, GWL_WNDPROC);
1135                                 SetWindowLong(hwndList, GWL_WNDPROC, (DWORD)ConnListBoxProc);
1136                         }
1137
1138                         break;
1139
1140           case WM_COMMAND:
1141                         switch( wParam )
1142                         {
1143                                 case IDC_CHECK1:                // start server
1144                                                 if (getButtonState(IDC_CHECK1))
1145                                                 {
1146                                                         //SendDlgItemMessage(hwnd, IDC_CHECK1,WM_SETTEXT, 0, (LPARAM)"Deactivate");
1147
1148                                                         SendDlgItemMessage(hwnd, IDC_EDIT3,WM_GETTEXT, 64, (LONG)servMgr->password);
1149
1150                                                         servMgr->serverHost.port = (unsigned short)getEditInt(IDC_EDIT1);
1151                                                         servMgr->setMaxRelays(getEditInt(IDC_MAXRELAYS));
1152
1153
1154                                                         enableControl(IDC_EDIT1,false);
1155                                                         enableControl(IDC_EDIT3,false);
1156                                                         enableControl(IDC_MAXRELAYS,false);
1157                                                         enableControl(IDC_BUTTON8,true);
1158                                                         enableControl(IDC_BUTTON11,true);
1159                                                         enableControl(IDC_BUTTON10,true);
1160
1161                                                         //writeSettings();
1162                                                         servMgr->autoServe = true;
1163
1164                                                         setEditStr(IDC_CHECK1,"Enabled");
1165
1166
1167                                                 }else{
1168                                                         //SendDlgItemMessage(hwnd, IDC_CHECK1,WM_SETTEXT, 0, (LPARAM)"Activate");
1169
1170                                                         servMgr->autoServe = false;
1171
1172                                                         enableControl(IDC_EDIT1,true);
1173                                                         enableControl(IDC_EDIT3,true);
1174                                                         enableControl(IDC_MAXRELAYS,true);
1175                                                         enableControl(IDC_BUTTON8,false);
1176                                                         enableControl(IDC_BUTTON11,false);
1177                                                         enableControl(IDC_BUTTON10,false);
1178
1179                                                         setEditStr(IDC_CHECK1,"Disabled");
1180
1181                                                 }
1182                                                 setControls(true);
1183
1184                                         break;
1185                                 case IDC_CHECK11:               // DJ message
1186                                         if (getButtonState(IDC_CHECK11))
1187                                         {
1188                                                 enableControl(IDC_EDIT9,false);
1189                                                 SendDlgItemMessage(hwnd, IDC_EDIT9,WM_GETTEXT, 128, (LONG)chanMgr->broadcastMsg.cstr());
1190                                         }else{
1191                                                 enableControl(IDC_EDIT9,true);
1192                                                 chanMgr->broadcastMsg.clear();
1193                                         }
1194                                         break;
1195                                 case IDC_LOGDEBUG:              // log debug
1196                                         servMgr->showLog = getButtonState(wParam) ? servMgr->showLog|(1<<LogBuffer::T_DEBUG) : servMgr->showLog&~(1<<LogBuffer::T_DEBUG);
1197                                         break;
1198                                 case IDC_LOGERRORS:             // log errors
1199                                         servMgr->showLog = getButtonState(wParam) ? servMgr->showLog|(1<<LogBuffer::T_ERROR) : servMgr->showLog&~(1<<LogBuffer::T_ERROR);
1200                                         break;
1201                                 case IDC_LOGNETWORK:            // log network
1202                                         servMgr->showLog = getButtonState(wParam) ? servMgr->showLog|(1<<LogBuffer::T_NETWORK) : servMgr->showLog&~(1<<LogBuffer::T_NETWORK);
1203                                         break;
1204                                 case IDC_LOGCHANNELS:           // log channels
1205                                         servMgr->showLog = getButtonState(wParam) ? servMgr->showLog|(1<<LogBuffer::T_CHANNEL) : servMgr->showLog&~(1<<LogBuffer::T_CHANNEL);
1206                                         break;
1207                                 case IDC_CHECK9:                // pause log
1208                                         servMgr->pauseLog = getButtonState(wParam);
1209                                         break;
1210                                 case IDC_CHECK2:                // start outgoing
1211
1212                                         if (getButtonState(IDC_CHECK2))
1213                                         {
1214
1215                                                 SendDlgItemMessage(hwnd, IDC_COMBO1,WM_GETTEXT, 128, (LONG)servMgr->connectHost);
1216                                                 servMgr->autoConnect = true;
1217                                                 //SendDlgItemMessage(hwnd, IDC_CHECK2,WM_SETTEXT, 0, (LPARAM)"Disconnect");
1218                                                 enableControl(IDC_COMBO1,false);
1219                                         }else{
1220                                                 servMgr->autoConnect = false;
1221                                                 //SendDlgItemMessage(hwnd, IDC_CHECK2,WM_SETTEXT, 0, (LPARAM)"Connect");
1222                                                 enableControl(IDC_COMBO1,true);
1223                                         }
1224                                         break;
1225                                 case IDC_BUTTON11:              // broadcast
1226                                         {
1227                                                 Host sh = servMgr->serverHost;
1228                                                 if (sh.isValid())
1229                                                 {
1230                                                         char cmd[256];
1231                                                         sprintf(cmd,"http://localhost:%d/admin?page=broadcast",sh.port);
1232                                                         ShellExecute(hwnd, NULL, cmd, NULL, NULL, SW_SHOWNORMAL);
1233                 
1234                                                 }else{
1235                                                         MessageBox(hwnd,"Server is not currently connected.\nPlease wait until you have a connection.","PeerCast",MB_OK);
1236                                                 }
1237                                         }
1238                                         break;
1239                                 case IDC_BUTTON8:               // play selected
1240                                         {
1241                                                 Channel *c = getListBoxChannel();
1242                                                 if (c){
1243                                                         chanMgr->playChannel(c->info);
1244                                                 }
1245                                         }
1246                                         break;
1247                                 case IDC_BUTTON7:               // advanced
1248                                         sys->callLocalURL("admin?page=settings",servMgr->serverHost.port);
1249                                         break;
1250                                 case IDC_BUTTON6:               // servent disconnect
1251                                         {
1252 /*                                              Servent *s = (Servent *)getListBoxSelData(statusID);
1253                                                 if (s)
1254                                                         s->thread.active = false;*/
1255                                                 Servent *s = getListBoxServent();
1256                                                 if (s){
1257                                                         s->thread.active = false;
1258                                                         s->thread.finish = true;
1259                                                 }
1260                                                 sleep_skip = true;
1261                                         }
1262                                         break;
1263                                 case IDC_BUTTON5:               // chan disconnect
1264                                         {
1265 /*                                              Channel *c = (Channel *)getListBoxSelData(chanID);
1266                                                 if (c)
1267                                                         c->thread.active = false;*/
1268
1269 //                                              Channel *c = chanMgr->findChannelByChannelID((int)getListBoxSelData(chanID));
1270                                                 Channel *c = getListBoxChannel();
1271                                                 if (c){
1272                                                         c->thread.active = false;
1273                                                         c->thread.finish = true;
1274                                                 }
1275                                                 sleep_skip = true;
1276                                         }
1277                                         break;
1278                                 case IDC_BUTTON3:               // chan bump
1279                                         {
1280 /*                                              Channel *c = (Channel *)getListBoxSelData(chanID);
1281                                                 if (c)
1282                                                         c->bump = true;*/
1283
1284 //                                              Channel *c = chanMgr->findChannelByChannelID((int)getListBoxSelData(chanID));
1285                                                 Channel *c = getListBoxChannel();
1286                                                 if (c){
1287                                                         c->bump = true;
1288                                                 }
1289                                         }
1290
1291                                         break;
1292                                 case IDC_BUTTON4:               // get channel 
1293                                         {
1294                                                 ChanHitList *chl = (ChanHitList *)getListBoxSelData(hitID);
1295                                                 if (chl)
1296                                                 {
1297                                                         if (!chanMgr->findChannelByID(chl->info.id))
1298                                                         {
1299                                                                 Channel *c = chanMgr->createChannel(chl->info,NULL);
1300                                                                 if (c)
1301                                                                         c->startGet();
1302                                                         }
1303                                                 }else{
1304                                                         MessageBox(hwnd,"Please select a channel","PeerCast",MB_OK);
1305                                                 }
1306                                         }
1307                                         break;
1308
1309                                         case IDC_BUTTON9:               //JP-EX chan keep
1310                                         {
1311 /*                                              Channel *c = (Channel *)getListBoxSelData(chanID);
1312                                                 if (c)
1313                                                 {
1314                                                         if (!c->stayConnected)
1315                                                         {
1316                                                                 //if (servMgr->getFirewall() == ServMgr::FW_OFF)
1317                                                                 c->stayConnected = true;
1318                                                         }       
1319                                                         else
1320                                                                 c->stayConnected = false;
1321                                                 }*/
1322
1323 //                                              Channel *c = chanMgr->findChannelByChannelID((int)getListBoxSelData(chanID));
1324                                                 Channel *c = getListBoxChannel();
1325                                                 if (c){
1326                                                         if (!c->stayConnected){
1327                                                                 c->stayConnected = true;
1328                                                         } else {
1329                                                                 c->stayConnected = false;
1330                                                         }
1331                                                 }
1332                                         }
1333                                         break;
1334
1335                                 case IDC_BUTTON1:               // clear log
1336                                         SendDlgItemMessage(guiWnd, logID, LB_RESETCONTENT, 0, 0);
1337                                         sys->logBuf->clear();   // for PCRaw (clear log)
1338                                         break;
1339
1340                                 case IDC_BUTTON2:               // find
1341                                         {
1342                                                 char str[64];
1343                                                 SendDlgItemMessage(hwnd, IDC_EDIT2,WM_GETTEXT, 64, (LONG)str);
1344                                                 SendDlgItemMessage(hwnd, hitID, LB_RESETCONTENT, 0, 0);
1345                                                 ChanInfo info;
1346                                                 info.init();
1347                                                 info.name.set(str);
1348                                                 chanMgr->startSearch(info);
1349                                         }
1350                                         break;
1351
1352                         }
1353                         break;
1354
1355                 case WM_MEASUREITEM:
1356                         if ((UINT) wParam==IDC_LIST3){
1357                                 LPMEASUREITEMSTRUCT lpMI = (LPMEASUREITEMSTRUCT)lParam;
1358                                 lpMI->itemHeight = 12;
1359                         } else if ((UINT) wParam==IDC_LIST2){
1360                                 LPMEASUREITEMSTRUCT lpMI = (LPMEASUREITEMSTRUCT)lParam;
1361                                 lpMI->itemHeight = 12;
1362                         }
1363                         break;
1364                 case WM_DRAWITEM:
1365                         if  ((UINT) wParam==IDC_LIST3)
1366                         {
1367                                 LPDRAWITEMSTRUCT _DrawItem=(LPDRAWITEMSTRUCT)lParam;
1368                                 HBRUSH   hBrush;
1369                                 ListData *ld;
1370                                 unsigned int idx = 0;
1371                                 bool flg = false;
1372
1373                                 ld = list_top;
1374                                 while(ld){
1375                                         if (_DrawItem->itemID == idx){
1376                                                 flg = true;
1377                                                 break;
1378                                         }
1379                                         ld = ld->next;
1380                                         idx++;
1381                                 }
1382                                 if ((_DrawItem->itemState) & (ODS_SELECTED))
1383                                 {
1384                                         hBrush=CreateSolidBrush(RGB(49,106,197));
1385                                 }
1386                                 else  
1387                                 {
1388                                         hBrush=CreateSolidBrush(RGB(255,255,255));
1389                                 }
1390                                 FillRect(_DrawItem->hDC,&_DrawItem->rcItem,hBrush);                   
1391                                 DeleteObject(hBrush);
1392                                 if (flg){
1393                                         char buf[256];
1394                                         if (ld->status == Channel::S_RECEIVING){
1395                                                 if (ld->chDisp.status == Channel::S_RECEIVING){
1396                                                         if (ld->chDisp.relay){
1397                                                                 /* relay ok */
1398                                                                 SetTextColor(_DrawItem->hDC,RGB(0,255,0));
1399                                                                 SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1400                                                         } else {
1401                                                                 /* no more relay */
1402                                                                 if (ld->chDisp.numRelays){
1403                                                                         /* relay full */
1404                                                                         SetTextColor(_DrawItem->hDC,RGB(0,0,255));
1405                                                                         SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1406                                                                 } else {
1407                                                                         /* relay ng */
1408                                                                         SetTextColor(_DrawItem->hDC,RGB(255,0,255));
1409                                                                         SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1410                                                                 }
1411                                                         }
1412                                                 } else {
1413                                                         /* status unmatch */
1414                                                         SetTextColor(_DrawItem->hDC,RGB(0,0,0));
1415                                                         SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1416                                                 }
1417                                                 TextOut(_DrawItem->hDC,
1418                                                         _DrawItem->rcItem.left,
1419                                                         _DrawItem->rcItem.top,
1420                                                         "\81¡",
1421                                                         2);
1422                                         } else {
1423                                                 SetTextColor(_DrawItem->hDC,RGB(0,0,0));
1424                                                 SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1425                                                 TextOut(_DrawItem->hDC,
1426                                                                 _DrawItem->rcItem.left,
1427                                                                 _DrawItem->rcItem.top,
1428                                                                 "\81 ",
1429                                                                 2);
1430                                         }
1431                                         if ((_DrawItem->itemState) & (ODS_SELECTED))
1432                                         {
1433                                                 SetTextColor(_DrawItem->hDC,RGB(255,255,255));
1434                                                 SetBkColor(_DrawItem->hDC,RGB(49,106,197)) ;
1435                                         } else {
1436                                                 SetTextColor(_DrawItem->hDC,RGB(0,0,0));
1437                                                 SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1438                                         }
1439                                         if (servMgr->getFirewall() == ServMgr::FW_ON){
1440                                                 SetTextColor(_DrawItem->hDC,RGB(255,0,0));
1441                                         } else if (ld->bTracker && (ld->status == Channel::S_RECEIVING)){
1442                                                 if ((_DrawItem->itemState) & (ODS_SELECTED))
1443                                                 {
1444                                                         SetTextColor(_DrawItem->hDC,RGB(0,255,0));
1445                                                         SetBkColor(_DrawItem->hDC,RGB(49,106,197)) ;
1446                                                 } else {
1447                                                         SetTextColor(_DrawItem->hDC,RGB(0,128,0));
1448                                                 }
1449                                         }
1450
1451                                         TextOut(_DrawItem->hDC,
1452                                                         _DrawItem->rcItem.left + 12,
1453                                                         _DrawItem->rcItem.top,
1454                                                         ld->name,
1455                                                         strlen(ld->name));
1456 /*                                      sprintf(buf, "- %4dkbps -", ld->bitRate);
1457                                         TextOut(_DrawItem->hDC,
1458                                                         _DrawItem->rcItem.left + 12 + 118,
1459                                                         _DrawItem->rcItem.top,
1460                                                         buf,
1461                                                         strlen(buf));
1462                                         TextOut(_DrawItem->hDC,
1463                                                         _DrawItem->rcItem.left + 12 + 118 + 80,
1464                                                         _DrawItem->rcItem.top,
1465                                                         ld->statusStr,
1466                                                         strlen(ld->statusStr));
1467                                         sprintf(buf, "- %3d/%3d - [%3d/%3d] -", ld->totalListeners, ld->totalRelays, ld->localListeners, ld->localRelays);
1468                                         TextOut(_DrawItem->hDC,
1469                                                         _DrawItem->rcItem.left + 12 + 118 + 80 + 80,
1470                                                         _DrawItem->rcItem.top,
1471                                                         buf,
1472                                                         strlen(buf));
1473                                         strcpy(buf, ld->stayConnected?"YES":"NO");
1474                                         TextOut(_DrawItem->hDC,
1475                                                         _DrawItem->rcItem.left + 12 + 118 + 80 + 80 + 130,
1476                                                         _DrawItem->rcItem.top,
1477                                                         buf,
1478                                                         strlen(buf));*/
1479                                         sprintf(buf, "- %4dkbps - %s - %3d/%3d - [%3d/%3d] - %s",
1480                                                 ld->bitRate,
1481                                                 ld->statusStr,
1482                                                 ld->totalListeners,
1483                                                 ld->totalRelays,
1484                                                 ld->localListeners,
1485                                                 ld->localRelays,
1486                                                 ld->stayConnected?"YES":"NO");
1487                                         TextOut(_DrawItem->hDC,
1488                                                         _DrawItem->rcItem.left + 12 + 118,
1489                                                         _DrawItem->rcItem.top,
1490                                                         buf,
1491                                                         strlen(buf));
1492                                 }
1493                         } else if  ((UINT) wParam==IDC_LIST2) {
1494                                 LPDRAWITEMSTRUCT _DrawItem=(LPDRAWITEMSTRUCT)lParam;
1495                                 HBRUSH   hBrush;
1496                                 ServentData *sd;
1497                                 unsigned int idx = 0;
1498                                 bool flg = false;
1499
1500                                 sd_lock.on();
1501
1502                                 sd = servent_top;
1503                                 while(sd){
1504                                         if (_DrawItem->itemID == idx){
1505                                                 flg = true;
1506                                                 break;
1507                                         }
1508                                         sd = sd->next;
1509                                         idx++;
1510                                 }
1511                                 if (flg){
1512                                         char buf[256];
1513                                         char hostName[64];
1514                                         sd->h.toStr(hostName);
1515
1516                                         if ((_DrawItem->itemState) & (ODS_SELECTED))
1517                                         {
1518                                                 hBrush=CreateSolidBrush(RGB(49,106,197));
1519                                         }
1520                                         else  
1521                                         {
1522                                                 hBrush=CreateSolidBrush(RGB(255,255,255));
1523                                         }
1524                                         FillRect(_DrawItem->hDC,&_DrawItem->rcItem,hBrush);                   
1525                                         DeleteObject(hBrush);
1526
1527                                         if (sd->infoFlg){
1528                                                 if (sd->relay){
1529                                                         /* relay ok */
1530                                                         SetTextColor(_DrawItem->hDC,RGB(0,255,0));
1531 //                                                      SetBkColor(_DrawItem->hDC,RGB(255,255,255));
1532                                                 } else {
1533                                                         /* no more relay */
1534                                                         if (sd->numRelays){
1535                                                                 /* relay full */
1536                                                                 SetTextColor(_DrawItem->hDC,RGB(0,0,255));
1537 //                                                              SetBkColor(_DrawItem->hDC,RGB(255,255,255));
1538                                                         } else {
1539                                                                 /* relay ng */
1540                                                                 SetTextColor(_DrawItem->hDC,RGB(255,0,255));
1541 //                                                              SetBkColor(_DrawItem->hDC,RGB(255,255,255));
1542                                                         }
1543                                                 }
1544                                         } else {
1545                                                 /* no info */
1546                                                 SetTextColor(_DrawItem->hDC,RGB(0,0,0));
1547 //                                              SetBkColor(_DrawItem->hDC,RGB(255,255,255));
1548                                         }
1549
1550                                         if (sd->lastSkipTime + 120 > sys->getTime()){
1551                                                 SetBkColor(_DrawItem->hDC,RGB(128,128,128));
1552                                                 if (sd->type == Servent::T_RELAY){
1553                                                         sprintf(buf, "\81¥(%d)",sd->lastSkipCount);
1554                                                         TextOut(_DrawItem->hDC,
1555                                                         _DrawItem->rcItem.left,
1556                                                         _DrawItem->rcItem.top,
1557                                                         buf,
1558                                                         strlen(buf));
1559                                                 } else {
1560                                                         SetTextColor(_DrawItem->hDC,RGB(0,0,0));
1561                                                         sprintf(buf, "\81¤(%d)",sd->lastSkipCount);
1562                                                         TextOut(_DrawItem->hDC,
1563                                                                         _DrawItem->rcItem.left,
1564                                                                         _DrawItem->rcItem.top,
1565                                                                         buf,
1566                                                                         strlen(buf));
1567                                                 }
1568                                         } else {
1569                                                 SetBkColor(_DrawItem->hDC,RGB(255,255,255));
1570                                                 if (sd->type == Servent::T_RELAY){
1571                                                         TextOut(_DrawItem->hDC,
1572                                                         _DrawItem->rcItem.left,
1573                                                         _DrawItem->rcItem.top,
1574                                                         "\81¡",
1575                                                         2);
1576                                                 } else {
1577                                                         SetTextColor(_DrawItem->hDC,RGB(0,0,0));
1578                                                         SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1579                                                         TextOut(_DrawItem->hDC,
1580                                                                         _DrawItem->rcItem.left,
1581                                                                         _DrawItem->rcItem.top,
1582                                                                         "\81 ",
1583                                                                         2);
1584                                                 }
1585                                         }
1586
1587                                         if ((_DrawItem->itemState) & (ODS_SELECTED))
1588                                         {
1589                                                 SetTextColor(_DrawItem->hDC,RGB(255,255,255));
1590                                                 SetBkColor(_DrawItem->hDC,RGB(49,106,197)) ;
1591                                         } else {
1592                                                 SetTextColor(_DrawItem->hDC,RGB(0,0,0));
1593                                                 SetBkColor(_DrawItem->hDC,RGB(255,255,255)) ;
1594                                         }
1595                                         if (sd->firewalled){
1596                                                 if (!sd->numRelays){
1597                                                         SetTextColor(_DrawItem->hDC,RGB(255,0,0));
1598                                                 } else {
1599                                                         SetTextColor(_DrawItem->hDC,RGB(255,168,0));
1600                                                 }
1601                                         }
1602
1603                                         char buf2[16];
1604                                         if (sd->ver_ex_number){
1605                                                 sprintf(buf2, "(%c%c%04d)", sd->ver_ex_prefix[0], sd->ver_ex_prefix[1], sd->ver_ex_number);
1606                                         } else if (sd->vp_ver){
1607                                                 sprintf(buf2, "(VP%04d)", sd->vp_ver);
1608                                         } else {
1609                                                 buf2[0] = '\0';
1610                                         }
1611                                         if (sd->type == Servent::T_RELAY){
1612                                                 if (sd->status == Servent::S_CONNECTED){
1613                                                         sprintf(buf, "(%d)RELAYING-%ds  - %d/%d -  %s  -  %d - %s%s",
1614                                                                 sd->lastSkipCount,
1615                                                                 sd->tnum,
1616                                                                 sd->totalListeners, sd->totalRelays,
1617                                                                 hostName,
1618                                                                 sd->syncpos, sd->agent.cstr(), buf2
1619                                                                 );
1620                                                 } else {
1621                                                         sprintf(buf, "%s-%s-%ds  - %d/%d -  %s  -  %d - %s%s",
1622                                                                 sd->typeStr, sd->statusStr, sd->tnum,
1623                                                                 sd->totalListeners, sd->totalRelays,
1624                                                                 hostName,
1625                                                                 sd->syncpos, sd->agent.cstr(), buf2
1626                                                                 );
1627                                                 }
1628                                         } else if (sd->type == Servent::T_DIRECT){
1629                                                 sprintf(buf, "%s-%s-%ds  -  %s  -  %d - %s ",
1630                                                         sd->typeStr, sd->statusStr, sd->tnum,
1631                                                         hostName,
1632                                                         sd->syncpos, sd->agent.cstr()
1633                                                         );
1634                                         } else {
1635                                                 if (sd->status == Servent::S_CONNECTED){
1636                                                         sprintf(buf, "%s-%s-%ds  -  %s  -  %d/%d",
1637                                                                 sd->typeStr, sd->statusStr, sd->tnum,
1638                                                                 hostName,
1639                                                                 sd->syncpos, sd->agent.cstr()
1640                                                                 );
1641                                                 } else {
1642                                                         sprintf(buf, "%s-%s-%ds  -  %s",
1643                                                                 sd->typeStr, sd->statusStr, sd->tnum,
1644                                                                 hostName
1645                                                                 );
1646                                                 }
1647                                         }
1648                                         TextOut(_DrawItem->hDC,
1649                                                         _DrawItem->rcItem.left + 12,
1650                                                         _DrawItem->rcItem.top,
1651                                                         buf,
1652                                                         strlen(buf));
1653                                 }
1654                                 sd_lock.off();
1655                         }
1656                         break;
1657
1658                 case WM_SIZE:
1659                         MoveControls(lParam);
1660                         break;
1661
1662                 case WM_GETMINMAXINFO:
1663                 {
1664                         MINMAXINFO *pmmi = (MINMAXINFO *)lParam;
1665                         if ( pmmi )
1666                         {
1667                                 pmmi->ptMinTrackSize.x = 530;  // \8dÅ\8f¬\95\9d
1668                                 pmmi->ptMinTrackSize.y = 435;  // \8dÅ\8f¬\8d\82
1669                                 pmmi->ptMaxTrackSize.x = 530; // \8dÅ\91å\95\9d
1670                                 pmmi->ptMaxTrackSize.y = 1200;  // \8dÅ\91å\8d\82
1671                         }
1672                         return 0;
1673                 }
1674
1675 /*              case WM_NCHITTEST:
1676                 {
1677                         LRESULT lResult; // \83J\81[\83\\83\8b\88Ê\92u\94»\92è\8c\8b\89Ê
1678
1679                         // \95Ï\8a·\88ê\97\97\82É\82 \82ê\82Î\95Ï\8a·\82µ\82½\92l\82ð\95Ô\82·\81B
1680                         lResult = DefWindowProc( guiWnd, message, wParam, lParam );
1681                         for( int i = 0; i < 8; i++ )
1682                         {
1683                                 if ( lResult == trans[ i ].from )
1684                                 {
1685                                         return trans[ i ].to;
1686                                 }
1687                         }
1688 //                      return lResult;
1689 //                      return HTCAPTION;
1690 //                      return 0;
1691                 }*/
1692                 
1693                 case WM_CLOSE:
1694                         GetWindowPlacement(hwnd, &winPlace);
1695                         guiFlg = true;
1696                         DestroyWindow( hwnd );
1697                         break;
1698
1699                 case WM_DESTROY:
1700                         GetWindowPlacement(hwnd, &winPlace);
1701                         guiFlg = true;
1702                         guiThread.active = false;
1703 //                      guiThread.lock();
1704                         guiWnd = NULL;
1705 //                      guiThread.unlock();
1706                         EndDialog(hwnd, LOWORD(wParam));
1707                         break;
1708
1709                 //default:
1710                         // do nothing
1711                         //return DefDlgProc(hwnd, message, wParam, lParam);             // this recurses for ever
1712                         //return DefWindowProc(hwnd, message, wParam, lParam);  // this stops window messages
1713    }
1714    return 0;
1715 }