OSDN Git Service

115701666083bb8ad2670b80a8b8723901b67dde
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast / ui / win32 / simple / Simple.cpp
1 // ------------------------------------------------
2 // File : simple.cpp
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //              Simple tray icon interface to PeerCast, mostly win32 related stuff.
7 //              
8 // (c) 2002 peercast.org
9 // ------------------------------------------------
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
14
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 // ------------------------------------------------
20
21 #include <windows.h>
22 #include <direct.h> 
23 #include "stdafx.h"
24 #include "resource.h"
25 #include "gui.h"
26 #include "channel.h"
27 #include "servent.h"
28 #include "servmgr.h"
29 #include "win32/wsys.h"
30 #include "peercast.h"
31 #include "simple.h"
32 #include "version2.h"
33 #include "gdiplus.h"
34 #include "time.h"
35 #ifdef _DEBUG
36 #include "chkMemoryLeak.h"
37 #define DEBUG_NEW new(__FILE__, __LINE__)
38 #define new DEBUG_NEW
39 #endif
40
41 #define MAX_LOADSTRING 100
42
43 #define PLAY_CMD 7000
44 #define RELAY_CMD 8000
45 #define INFO_CMD 9000
46 #define URL_CMD 10000
47
48 #define MAX_CHANNELS 999
49
50
51 extern "C"
52 {
53         void loadIcons(HINSTANCE hInstance, HWND hWnd);
54 };
55
56 UINT g_iTaskbarCreated = ~0;    // for PCRaw (tray icon)
57
58 // PeerCast globals
59
60 static int currNotify=0;
61 String iniFileName;
62 HWND guiWnd;
63 HWND mainWnd;
64 static HMENU trayMenu = NULL,ltrayMenu = NULL;  // for PCRaw (tray icon)
65 bool showGUI=true;
66 bool allowMulti=false;
67 bool killMe=false;
68 bool allowTrayMenu=true;
69 static bool winDistinctionNT=false;
70 int             seenNewVersionTime=0;
71 HICON icon1,icon2;
72 ChanInfo chanInfo;
73 bool chanInfoIsRelayed;
74 //GnuID lastPlayID;
75 String exePath;
76 ULONG_PTR gdiplusToken;
77
78 // \83v\83\8d\83g\83^\83C\83v\90é\8c¾
79 void createGUI(HWND);
80
81 // ---------------------------------
82 Sys * APICALL MyPeercastInst::createSys()
83 {
84         return new WSys(mainWnd);
85 }
86 // ---------------------------------
87 const char * APICALL MyPeercastApp ::getIniFilename()
88 {
89         return iniFileName.cstr();
90 }
91
92 // ---------------------------------
93 const char *APICALL MyPeercastApp ::getClientTypeOS() 
94 {
95         return PCX_OS_WIN32;
96 }
97
98 // ---------------------------------
99 const char * APICALL MyPeercastApp::getPath()
100 {
101         return exePath.cstr();
102 }
103
104 // --------------------------------- JP-EX
105 void    APICALL MyPeercastApp ::openLogFile()
106 {
107         logFile.openWriteReplace("log.txt");
108 }
109 // --------------------------------- JP-EX
110 void    APICALL MyPeercastApp ::getDirectory()
111 {
112         char path_buffer[256],drive[32],dir[128];
113         GetModuleFileName(NULL,path_buffer,255);
114         _splitpath(path_buffer,drive,dir,NULL,NULL);
115         sprintf(servMgr->modulePath,"%s%s",drive,dir);
116 }
117 // --------------------------------- JP-EX
118 bool    APICALL MyPeercastApp ::clearTemp()
119 {
120         if (servMgr->clearPLS)
121                 return true;
122
123         return false;
124 }
125
126
127 class NOTIFYICONDATA2
128 {
129 public:
130         DWORD cbSize; // DWORD
131         HWND hWnd; // HWND
132         UINT uID; // UINT
133         UINT uFlags; // UINT
134         UINT uCallbackMessage; // UINT
135         HICON hIcon; // HICON
136         char szTip[128]; // char[128]
137         DWORD dwState; // DWORD
138         DWORD dwStateMask; // DWORD
139         char szInfo[256]; // char[256]
140         UINT uTimeoutOrVersion; // UINT
141         char szInfoTitle[64]; // char[64]
142         DWORD dwInfoFlags; // DWORD
143         //GUID guidItem; > IE 6
144 };
145
146 NOTIFYICONDATA2 trayIcon;
147
148
149 // Global Variables:
150 HINSTANCE hInst;                                                                // current instance
151 TCHAR szTitle[MAX_LOADSTRING];                                                          // The title bar text
152 TCHAR szWindowClass[MAX_LOADSTRING];                                                            // The title bar text
153 TCHAR szWindowClass2[MAX_LOADSTRING];                                                           // The title bar text
154
155 // Foward declarations of functions included in this code module:
156 ATOM                            MyRegisterClass(HINSTANCE hInstance);
157 ATOM                            MyRegisterClass2(HINSTANCE hInstance);
158 ATOM                            MyRegisterClass3(HINSTANCE hInstance);
159 BOOL                            InitInstance(HINSTANCE, int);
160 LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
161 LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);
162 LRESULT CALLBACK        ChanInfoProc(HWND, UINT, WPARAM, LPARAM);
163
164 void setTrayIcon(int type, const char *,const char *,bool);
165 void flipNotifyPopup(int id, ServMgr::NOTIFY_TYPE nt);
166
167
168 HWND chWnd=NULL;
169
170 // --------------------------------------------------
171 void LOG2(const char *fmt,...)
172 {
173         va_list ap;
174         va_start(ap, fmt);
175         char str[4096];
176         vsprintf(str,fmt,ap);
177         OutputDebugString(str);
178         va_end(ap);     
179 }
180
181
182
183 // ---------------------------------------
184 int APIENTRY WinMain(HINSTANCE hInstance,
185                      HINSTANCE hPrevInstance,
186                      LPSTR     lpCmdLine,
187                      int       nCmdShow)
188 {
189 #ifdef _DEBUG
190         // memory leak check
191         ::_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
192 #endif
193
194         char tmpURL[8192];
195         tmpURL[0]=0;
196         char *chanURL=NULL;
197
198         hInst = hInstance;
199
200         iniFileName.set(".\\peercast.ini");
201
202         WIN32_FIND_DATA fd; //JP-EX
203         HANDLE hFind; //JP-EX
204
205         OSVERSIONINFO osInfo; //JP-EX
206         osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); //JP-EX
207         GetVersionEx(&osInfo);
208         if (osInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
209                 winDistinctionNT = true;
210         else
211                 winDistinctionNT = false;
212
213         // off by default now
214         showGUI = false;
215
216         if (strlen(lpCmdLine) > 0)
217         {
218                 char *p;
219                 if ((p = strstr(lpCmdLine,"-inifile"))!=NULL) 
220                         iniFileName.setFromString(p+8);
221
222                 if (strstr(lpCmdLine,"-zen")) 
223                         showGUI = false;
224
225                 if (strstr(lpCmdLine,"-multi")) 
226                         allowMulti = true;
227
228                 if (strstr(lpCmdLine,"-kill")) 
229                         killMe = true;
230
231                 if ((p = strstr(lpCmdLine,"-url"))!=NULL)
232                 {
233                         p+=4;
234                         while (*p)
235                         {
236                                 if (*p=='"')
237                                 {
238                                         p++;
239                                         break;
240                                 }                               
241                                 if (*p != ' ')
242                                         break;
243                                 p++;
244                         }
245                         if (*p)
246                                 strncpy(tmpURL,p,sizeof(tmpURL)-1);
247                 }
248         }
249
250         // get current path
251         {
252                 exePath = iniFileName;
253                 char *s = exePath.cstr();
254                 char *end = NULL;
255                 while (*s)
256                 {
257                         if (*s++ == '\\')
258                                 end = s;
259                 }
260                 if (end)
261                         *end = 0;
262         }
263
264         
265         if (strnicmp(tmpURL,"peercast://",11)==0)
266         {
267                 if (strnicmp(tmpURL+11,"pls/",4)==0)
268                         chanURL = tmpURL+11+4;
269                 else
270                         chanURL = tmpURL+11;
271                 showGUI = false;
272         }
273
274
275         MSG msg;
276         HACCEL hAccelTable;
277
278         // Initialize global strings
279         //LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
280         //LoadString(hInstance, IDC_APP_TITLE, szWindowClass, MAX_LOADSTRING);
281
282         strcpy(szTitle,"PeerCast");
283         strcpy(szWindowClass,"PeerCast");
284         strcpy(szWindowClass2,"Main");
285
286         if (!allowMulti)
287         {
288                 HANDLE mutex = CreateMutex(NULL,TRUE,szWindowClass);
289                 
290                 if (GetLastError() == ERROR_ALREADY_EXISTS)
291                 {
292                         HWND oldWin = FindWindow(szWindowClass,NULL);
293                         if (oldWin)
294                         {
295                                                 SendMessage(oldWin,WM_SHOWGUI,0,0);
296                                 if (killMe)
297                                 {
298                                         SendMessage(oldWin,WM_DESTROY,0,0);
299                                         return 0;
300                                 }
301
302                                 if (chanURL)
303                                 {
304                                         COPYDATASTRUCT copy;
305                                         copy.dwData = WM_PLAYCHANNEL;
306                                         copy.cbData = strlen(chanURL)+1;                        // plus null term
307                                         copy.lpData = chanURL;
308                                         SendMessage(oldWin,WM_COPYDATA,NULL,(LPARAM)&copy);
309                                 }else{
310                                         if (showGUI)
311                                                 SendMessage(oldWin,WM_SHOWGUI,0,0);
312                                 }
313                         }
314                         return 0;
315                 }
316         }
317
318         if (killMe)
319                 return 0;
320         
321         MyRegisterClass(hInstance);
322         MyRegisterClass2(hInstance);
323
324         // Perform application initialization:
325         if (!InitInstance (hInstance, nCmdShow)) 
326                 return FALSE;
327
328         peercastInst = new MyPeercastInst();
329         peercastApp = new MyPeercastApp();
330
331         peercastInst->init();
332
333         LOG_DEBUG("Set OS Type: %s",winDistinctionNT?"WinNT":"Win9x");
334
335         if (peercastApp->clearTemp()) //JP-EX
336         {
337                 DeleteFile("play.pls");
338                 hFind = FindFirstFile("*.asx",&fd);
339                 if (hFind != INVALID_HANDLE_VALUE)
340                 {
341                         do
342                         {
343                                 DeleteFile((char *)&fd.cFileName);
344                         }
345                         while (FindNextFile(hFind,&fd));
346
347                         FindClose(hFind);
348                 }
349         }
350
351         if (chanURL)
352         {
353                 ChanInfo info;
354                 servMgr->procConnectArgs(chanURL,info);
355                 chanMgr->findAndPlayChannel(info,false);
356         }
357
358         hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SIMPLE);
359
360         // setup menu notifes
361         int mask = peercastInst->getNotifyMask();
362         if (mask & ServMgr::NT_PEERCAST)
363                 CheckMenuItem(trayMenu,ID_POPUP_SHOWMESSAGES_PEERCAST,MF_CHECKED|MF_BYCOMMAND);
364         if (mask & ServMgr::NT_BROADCASTERS)
365                 CheckMenuItem(trayMenu,ID_POPUP_SHOWMESSAGES_BROADCASTERS,MF_CHECKED|MF_BYCOMMAND);
366         if (mask & ServMgr::NT_TRACKINFO)
367                 CheckMenuItem(trayMenu,ID_POPUP_SHOWMESSAGES_TRACKINFO,MF_CHECKED|MF_BYCOMMAND);
368
369         if (servMgr->startWithGui)
370         {
371                 createGUI((HWND)0);
372         }
373
374         // Main message loop:
375         while (GetMessage(&msg, NULL, 0, 0)) 
376         {
377                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
378                 {
379                         TranslateMessage(&msg);
380                         DispatchMessage(&msg);
381                 }
382         }
383
384     Shell_NotifyIcon(NIM_DELETE, (NOTIFYICONDATA*)&trayIcon);
385
386         peercastInst->saveSettings();
387         peercastInst->quit();
388
389         Gdiplus::GdiplusShutdown(gdiplusToken);
390
391         return msg.wParam;
392 }
393
394
395
396 //
397 //  FUNCTION: MyRegisterClass()
398 //
399 //  PURPOSE: Registers the window class.
400 //
401 //  COMMENTS:
402 //
403 //    This function and its usage is only necessary if you want this code
404 //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
405 //    function that was added to Windows 95. It is important to call this function
406 //    so that the application will get 'well formed' small icons associated
407 //    with it.
408 //
409 ATOM MyRegisterClass(HINSTANCE hInstance)
410 {
411         WNDCLASSEX wcex;
412
413         wcex.cbSize = sizeof(WNDCLASSEX); 
414
415         wcex.style                      = CS_HREDRAW | CS_VREDRAW;
416         wcex.lpfnWndProc        = (WNDPROC)WndProc;
417         wcex.cbClsExtra         = 0;
418         wcex.cbWndExtra         = 0;
419         wcex.hInstance          = hInstance;
420         wcex.hIcon                      = LoadIcon(hInstance, (LPCTSTR)IDI_SIMPLE);
421         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
422         wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW+1);
423 //      wcex.lpszMenuName       = (LPCSTR)IDC_SIMPLE;
424         wcex.lpszMenuName       = NULL;
425         wcex.lpszClassName      = szWindowClass;
426         wcex.hIconSm            = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
427
428         return RegisterClassEx(&wcex);
429 }
430
431 ATOM MyRegisterClass2(HINSTANCE hInstance)
432 {
433         WNDCLASSEX wcex;
434         
435         wcex.cbSize = sizeof(WNDCLASSEX); 
436
437         wcex.style                      = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
438         wcex.lpfnWndProc        = (WNDPROC)GUIProc;
439         wcex.cbClsExtra         = 0;
440         wcex.cbWndExtra         = 0;
441         wcex.hInstance          = hInstance;
442         wcex.hIcon                      = LoadIcon(hInstance, (LPCTSTR)IDI_SIMPLE);
443         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
444         wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW+1);
445 //      wcex.lpszMenuName       = (LPCSTR)IDC_SIMPLE;
446         wcex.lpszMenuName       = NULL;
447         wcex.lpszClassName      = szWindowClass2;
448         wcex.hIconSm            = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
449
450         return RegisterClassEx(&wcex);
451 }
452
453 //-----------------------------
454 void loadIcons(HINSTANCE hInstance, HWND hWnd)
455 {
456         icon1 = LoadIcon(hInstance, (LPCTSTR)IDI_SMALL);
457         icon2 = LoadIcon(hInstance, (LPCTSTR)IDI_SMALL2);
458
459     trayIcon.cbSize = sizeof(trayIcon);
460     trayIcon.hWnd = hWnd;
461     trayIcon.uID = 100;
462     trayIcon.uFlags = NIF_MESSAGE + NIF_ICON + NIF_TIP;
463     trayIcon.uCallbackMessage = WM_TRAYICON;
464     trayIcon.hIcon = icon1;
465     strcpy(trayIcon.szTip, "PeerCast");
466
467     Shell_NotifyIcon(NIM_ADD, (NOTIFYICONDATA*)&trayIcon);
468
469     //ShowWindow(hWnd, nCmdShow);
470     UpdateWindow(hWnd);
471
472         if(!trayMenu)   // for PCRaw (tray icon)
473                 trayMenu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_TRAYMENU));
474         if(!ltrayMenu)  // for PCRaw (tray icon)
475                 ltrayMenu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_LTRAYMENU));
476
477
478 }
479
480 //-----------------------------
481 //
482 //   FUNCTION: InitInstance(HANDLE, int)
483 //
484 //   PURPOSE: Saves instance handle and creates main window
485 //
486 //   COMMENTS:
487 //
488 //        In this function, we save the instance handle in a global variable and
489 //        create and display the main program window.
490 //
491 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
492 {
493         HWND hWnd;
494
495         hInst = hInstance; // Store instance handle in our global variable
496
497         hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
498           CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
499
500         if (!hWnd)
501         {
502           return FALSE;
503         }
504
505         mainWnd = hWnd;
506
507         g_iTaskbarCreated = RegisterWindowMessage("TaskbarCreated");    // for PCRaw (tray icon)
508
509         loadIcons(hInstance,hWnd);
510
511         using namespace Gdiplus;
512         GdiplusStartupInput gdiplusStartupInput;
513         GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
514
515         return TRUE;
516 }
517 //-----------------------------
518 //static String trackTitle;
519 //static String channelComment;
520
521 //-----------------------------
522 void channelPopup(const char *title, const char *msg, bool isPopup = true)
523 {
524         String both;
525
526         if (*title == '\0') return;
527         both.append(msg);
528         both.append(" (");
529         both.append(title);
530         both.append(")");
531
532         trayIcon.uFlags = NIF_ICON|NIF_TIP;
533         strncpy(trayIcon.szTip, both.cstr(),sizeof(trayIcon.szTip)-1);
534         trayIcon.szTip[sizeof(trayIcon.szTip)-1]=0;
535
536         if (isPopup) trayIcon.uFlags |= 16;
537         trayIcon.uTimeoutOrVersion = 10000;
538         strncpy(trayIcon.szInfo,msg,sizeof(trayIcon.szInfo)-1);
539         strncpy(trayIcon.szInfoTitle,title,sizeof(trayIcon.szInfoTitle)-1);
540                 
541         Shell_NotifyIcon(NIM_MODIFY, (NOTIFYICONDATA*)&trayIcon);
542 }
543 //-----------------------------
544 void clearChannelPopup()
545 {
546         trayIcon.uFlags = NIF_ICON|16;
547         trayIcon.uTimeoutOrVersion = 10000;
548     strncpy(trayIcon.szInfo,"",sizeof(trayIcon.szInfo)-1);
549         strncpy(trayIcon.szInfoTitle,"",sizeof(trayIcon.szInfoTitle)-1);
550         Shell_NotifyIcon(NIM_MODIFY, (NOTIFYICONDATA*)&trayIcon);
551 }
552
553 //-----------------------------
554 // PopupEntry
555 struct PopupEntry {
556         GnuID id;
557         String name;
558         String track;
559         String comment;
560         PopupEntry *next;
561 };
562 static PopupEntry *PEList = NULL;
563 static WLock PELock;
564
565 static void putPopupEntry(PopupEntry *pe)
566 {
567         PELock.on();
568         pe->next = PEList;
569         PEList = pe;
570         PELock.off();
571 }
572
573 static PopupEntry *getPopupEntry(GnuID id)
574 {
575         PELock.on();
576         PopupEntry *pe = PEList;
577         PopupEntry *prev = NULL;
578         while (pe) {
579                 if (id.isSame(pe->id)) {
580                         if (prev) prev->next = pe->next;
581                         else PEList = pe->next;
582                         PELock.off();
583                         pe->next = NULL;
584                         return pe;
585                 }
586                 prev = pe;
587                 pe = pe->next;
588         }
589         PELock.off();
590         return NULL;
591 }
592
593 static PopupEntry *getTopPopupEntry()
594 {
595         PopupEntry *p = NULL;
596         PELock.on();
597         if (PEList) {
598                 p = PEList;
599                 PEList = PEList->next;
600         }
601         PELock.off();
602         return p;
603 }
604
605 //-----------------------------
606 void    APICALL MyPeercastApp::channelStart(ChanInfo *info)
607 {
608
609 //      lastPlayID = info->id;
610 //
611 //      if(!isIndexTxt(info))   // for PCRaw (popup)
612 //              clearChannelPopup();
613
614         PopupEntry *pe = getPopupEntry(info->id);
615         if (!pe) {
616                 pe = new PopupEntry;
617                 pe->id = info->id;
618         }
619         if (!isIndexTxt(info))
620                 putPopupEntry(pe);
621         else
622                 delete pe;
623 }
624 //-----------------------------
625 void    APICALL MyPeercastApp::channelStop(ChanInfo *info)
626 {
627 //      if (info->id.isSame(lastPlayID))
628 //      {
629 //              lastPlayID.clear();
630 //
631 //              if(!isIndexTxt(info))   // for PCRaw (popup)
632 //                      clearChannelPopup();
633 //      }
634
635         PopupEntry *pe = getPopupEntry(info->id);
636         if (pe) delete pe;
637
638         pe = getTopPopupEntry();
639         if (!pe) {
640                 clearChannelPopup();
641         } else {
642                 if (ServMgr::NT_TRACKINFO & peercastInst->getNotifyMask())
643                 {
644                         String name,track; //JP-Patch
645                         name = pe->name; //JP-Patch
646                         track = pe->track; //JP-Patch
647                         name.convertTo(String::T_SJIS); //JP-Patch
648                         track.convertTo(String::T_SJIS); //JP-Patch
649                         clearChannelPopup();
650                 //      channelPopup(info->name.cstr(),trackTitle.cstr());
651                         channelPopup(name.cstr(),track.cstr(), false); //JP-Patch
652                 }
653                 putPopupEntry(pe);
654         }
655 }
656 //-----------------------------
657 void    APICALL MyPeercastApp::channelUpdate(ChanInfo *info)
658 {
659         if (info)
660         {
661                 PopupEntry *pe = getPopupEntry(info->id);
662                 if (!pe) return;
663
664                 String tmp;
665                 tmp.append(info->track.artist);
666                 tmp.append(" ");
667                 tmp.append(info->track.title);
668
669
670                 if (!tmp.isSame(pe->track))
671                 {
672                         pe->name = info->name;
673                         pe->track = tmp;
674                         if (ServMgr::NT_TRACKINFO & peercastInst->getNotifyMask())
675                         {
676                                 //trackTitle=tmp;
677                                 String name,track; //JP-Patch
678                                 name = info->name; //JP-Patch
679                                 track = tmp; //JP-Patch
680                                 name.convertTo(String::T_SJIS); //JP-Patch
681                                 track.convertTo(String::T_SJIS); //JP-Patch
682                                 if(!isIndexTxt(info))   // for PCRaw (popup)
683                                 {
684                                         clearChannelPopup();
685                                 //      channelPopup(info->name.cstr(),trackTitle.cstr());
686                                         channelPopup(name.cstr(),track.cstr()); //JP-Patch
687                                 }
688                         }
689                 } else if (!info->comment.isSame(pe->comment))
690                 {
691                         pe->name = info->name;
692                         pe->comment = info->comment;
693                         if (ServMgr::NT_BROADCASTERS & peercastInst->getNotifyMask())
694                         {
695                                 //channelComment = info->comment;
696                                 String name,comment; //JP-Patch
697                                 name = info->name; //JP-Patch
698                                 comment = info->comment; //JP-Patch
699                                 name.convertTo(String::T_SJIS); //JP-Patch
700                                 comment.convertTo(String::T_SJIS); //JP-Patch
701                                 if(!isIndexTxt(info))   // for PCRaw (popup)
702                                 {
703                                         clearChannelPopup();
704                                 //      channelPopup(info->name.cstr(),channelComment.cstr());
705                                         channelPopup(name.cstr(),comment.cstr());
706                                 }
707                         }
708                 }
709
710                 if (!isIndexTxt(info))
711                         putPopupEntry(pe);
712                 else
713                         delete pe;
714         }
715 }
716 //-----------------------------
717 void    APICALL MyPeercastApp::notifyMessage(ServMgr::NOTIFY_TYPE type, const char *msg)
718 {
719         static bool shownUpgradeAlert=false;
720
721         currNotify = type;
722
723         trayIcon.uFlags = 0;
724
725         if (!shownUpgradeAlert)
726         {
727             trayIcon.uFlags = NIF_ICON;
728
729                 if (type == ServMgr::NT_UPGRADE)
730                 {
731                         shownUpgradeAlert = true;
732                     trayIcon.hIcon = icon2;
733                 }else
734                 {
735                         trayIcon.hIcon = icon1; 
736                 }
737         }else
738         {
739                 if (type == ServMgr::NT_UPGRADE)
740                         return;
741         }
742
743         const char *title="";
744
745         switch(type)
746         {
747                 case ServMgr::NT_UPGRADE:
748                         title = "Upgrade alert";
749                         break;
750                 case ServMgr::NT_PEERCAST:
751                         title = "Message from PeerCast:";
752                         break;
753
754         }
755
756         if (type & peercastInst->getNotifyMask())
757         {
758                 trayIcon.uFlags |= 16;
759         trayIcon.uTimeoutOrVersion = 10000;
760         strncpy(trayIcon.szInfo,msg,sizeof(trayIcon.szInfo)-1);
761                 strncpy(trayIcon.szInfoTitle,title,sizeof(trayIcon.szInfoTitle)-1);
762             Shell_NotifyIcon(NIM_MODIFY, (NOTIFYICONDATA*)&trayIcon);
763         }
764 }
765 //-----------------------------
766
767 // createGUI()
768 //
769 void createGUI(HWND hWnd)
770 {
771         if (!guiWnd){
772                 guiWnd = ::CreateWindow(szWindowClass2,
773                         "Peercast-IM@S",
774                         WS_OVERLAPPEDWINDOW & ~(WS_MAXIMIZEBOX) /*| WS_VSCROLL | WS_HSCROLL*/,
775                         0,
776                         0,
777                         800,
778                         600,
779                         NULL,
780                         NULL,
781                         hInst,
782                         NULL);
783         }
784         ShowWindow(guiWnd,SW_SHOWNORMAL);
785
786         // \8e©\93®\82Å\8dÅ\91O\96Ê
787         if (servMgr->topmostGui)
788         {
789                 ::SetWindowPos(guiWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
790         }
791 }
792
793
794 // 
795 // addRelayedChannelsMenu(HMENU m)
796 // 
797 //
798 void addRelayedChannelsMenu(HMENU cm)
799 {
800         int cnt = GetMenuItemCount(cm);
801         for(int i=0; i<cnt-3; i++)
802                 DeleteMenu(cm,0,MF_BYPOSITION);
803
804         Channel *c = chanMgr->channel;
805         while(c)
806         {
807                 if (c->isActive())
808                 {
809                         char str[128],name[64];
810                         strncpy(name,c->info.name,32);
811                         name[32]=0;
812                         if (strlen(c->info.name) > 32)
813                                 strcat(name,"...");
814
815
816                         sprintf(str,"%s  (%d kb/s %s)",name,c->info.bitrate,ChanInfo::getTypeStr(c->info.contentType));
817                         //InsertMenu(cm,0,MF_BYPOSITION,RELAY_CMD+i,str);
818                 }
819                 c=c->next;
820         }
821 }
822
823 typedef int (*COMPARE_FUNC)(const void *,const void *);
824
825 static int compareHitLists(ChanHitList **c2, ChanHitList **c1)
826 {
827         return stricmp(c1[0]->info.name.cstr(),c2[0]->info.name.cstr());
828 }
829
830 static int compareChannels(Channel **c2, Channel **c1)
831 {
832         return stricmp(c1[0]->info.name.cstr(),c2[0]->info.name.cstr());
833 }
834
835 // 
836 // addAllChannelsMenu(HMENU m)
837 // 
838 //
839 void addAllChannelsMenu(HMENU cm)
840 {
841         int cnt = GetMenuItemCount(cm);
842 /*      for(int i=0; i<cnt-2; i++)
843                 DeleteMenu(cm,0,MF_BYPOSITION);*/
844
845         for(int i=0; i<cnt; i++)
846                 DeleteMenu(cm,0,MF_BYPOSITION);
847
848         HMENU yMenu = CreatePopupMenu();
849         if (!servMgr->rootHost2.isEmpty()){
850                 InsertMenu(yMenu,0,MF_BYPOSITION,ID_POPUP_YELLOWPAGES2,servMgr->rootHost2);
851         }
852         if (!servMgr->rootHost.isEmpty()){
853                 InsertMenu(yMenu,0,MF_BYPOSITION,ID_POPUP_YELLOWPAGES1,servMgr->rootHost);
854         }
855
856         InsertMenu(cm,0,MF_BYPOSITION|MF_POPUP,(UINT)yMenu,"\83C\83G\83\8d\81[\83y\81[\83W");
857         InsertMenu(cm,0,MF_BYPOSITION|MF_SEPARATOR,NULL,NULL);
858         // add channels to menu
859         int numActive=0;
860         Channel *ch = chanMgr->channel;
861         while(ch)
862         {
863                 char str[128],name[64];
864                 String sjis; //JP-Patch
865                 sjis = ch->info.name; //JP-Patch
866                 sjis.convertTo(String::T_SJIS); //JP-Patch
867                 strncpy(name,sjis.cstr(),32);
868                 //strncpy(name,ch->info.name,32);
869                 name[32]=0;
870                 //if (strlen(ch->info.name) > 32)
871                 if (strlen(sjis.cstr()) > 32) //JP-Patch
872                         strcat(name,"...");
873
874                 sprintf(str,"%s  (%d kb/s %s)",name,ch->info.bitrate,ChanInfo::getTypeStr(ch->info.contentType));
875
876                 HMENU opMenu = CreatePopupMenu();
877                 InsertMenu(opMenu,0,MF_BYPOSITION,INFO_CMD+numActive,"Info");
878                 if (ch->info.url.isValidURL())
879                         InsertMenu(opMenu,0,MF_BYPOSITION,URL_CMD+numActive,"URL");
880                 InsertMenu(opMenu,0,MF_BYPOSITION,PLAY_CMD+numActive,"Play");
881
882                 UINT fl = MF_BYPOSITION|MF_POPUP;
883                 if (ch)
884                         fl |= (ch->isPlaying()?MF_CHECKED:0);
885
886                 InsertMenu(cm,0,fl,(UINT)opMenu,str);
887                 
888                 numActive++;
889
890                 ch=ch->next;
891         }
892
893
894         //if (!numActive)
895         //              InsertMenu(cm,0,MF_BYPOSITION,0,"<No channels>");
896
897
898
899
900 }
901
902
903 // 
904 // flipNotifyPopup(id, flag)
905 void flipNotifyPopup(int id, ServMgr::NOTIFY_TYPE nt)
906 {
907         int mask = peercastInst->getNotifyMask();
908
909         mask ^= nt;
910         if (mask & nt)
911                 CheckMenuItem(trayMenu,id,MF_CHECKED|MF_BYCOMMAND);
912         else
913                 CheckMenuItem(trayMenu,id,MF_UNCHECKED|MF_BYCOMMAND);
914
915         peercastInst->setNotifyMask(mask);
916         peercastInst->saveSettings();
917 }
918  
919
920 static void showHTML(const char *file)
921 {
922         char url[256];
923         sprintf(url,"%s/%s",servMgr->htmlPath,file);                                    
924
925 //      sys->callLocalURL(url,servMgr->serverHost.port);
926         sys->callLocalURL(url,  // for PCRaw (url)
927                 (servMgr->allowServer1&Servent::ALLOW_HTML)?(servMgr->serverHost.port):(servMgr->serverHost.port+1));
928 }
929
930 static ChanInfo getChannelInfo(int index)
931 {
932         Channel *c = chanMgr->findChannelByIndex(index);
933         if (c)
934                 return c->info;
935
936         ChanInfo info;
937         return info;
938 }
939
940 //
941 //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
942 //
943 //  PURPOSE:  Processes messages for the main window.
944 //
945 //  WM_COMMAND  - process the application menu
946 //  WM_PAINT    - Paint the main window
947 //  WM_DESTROY  - post a quit message and return
948 //
949 //
950 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
951 {
952         int wmId, wmEvent;
953         POINT point;
954         char buf[1024];
955
956         if(message == g_iTaskbarCreated)        // for PCRaw (tray icon)
957                 loadIcons(hInst, hWnd);
958  
959         switch (message) 
960         {
961                 case WM_SHOWGUI:
962                         createGUI(hWnd);
963                         break;
964
965
966                 case WM_TRAYICON:
967                         switch((UINT)lParam) 
968                         {
969                                 case WM_LBUTTONDOWN:
970                                         if (allowTrayMenu)
971                                                 SendMessage(hWnd,WM_SHOWMENU,2,0);
972                                         SetForegroundWindow(hWnd);    
973                                         break;
974                                 case WM_RBUTTONDOWN:
975                                         if (allowTrayMenu)
976                                                 SendMessage(hWnd,WM_SHOWMENU,1,0);
977                                         SetForegroundWindow(hWnd);    
978                                         break;
979                                 case WM_LBUTTONDBLCLK:
980                                         createGUI(hWnd);
981                                         break;
982                         }
983                         break;
984
985                 case WM_COPYDATA:
986                         {
987                                 COPYDATASTRUCT *pc = (COPYDATASTRUCT *)lParam;
988                                 LOG_DEBUG("URL request: %s",pc->lpData);
989                                 if (pc->dwData == WM_PLAYCHANNEL)
990                                 {
991                                         ChanInfo info;
992                                         servMgr->procConnectArgs((char *)pc->lpData,info);
993                                         chanMgr->findAndPlayChannel(info,false);
994                                 }
995                                 //sys->callLocalURL((const char *)pc->lpData,servMgr->serverHost.port);
996                         }
997                         break;
998                 case WM_GETPORTNUMBER:
999                         {
1000                                 int port;
1001                                 port=servMgr->serverHost.port;
1002                                 ReplyMessage(port);
1003                         }
1004                         break;
1005
1006                 case WM_SHOWMENU:
1007                         {
1008                                 if (servMgr->saveGuiPos){
1009                                         CheckMenuItem(trayMenu, ID_POPUP_SAVE_GUI_POS, MF_CHECKED|MF_BYCOMMAND);
1010                                 } else {
1011                                         CheckMenuItem(trayMenu, ID_POPUP_SAVE_GUI_POS, MF_UNCHECKED|MF_BYCOMMAND);
1012                                 }
1013
1014                                 // \8e©\93®GUI/\8dÅ\91O\96Ê\8b@\94\
1015                                 if (servMgr->topmostGui)
1016                                 {
1017                                         CheckMenuItem(trayMenu, ID_POPUP_TOPMOST, MF_CHECKED|MF_BYCOMMAND);
1018                                 } else
1019                                 {
1020                                         CheckMenuItem(trayMenu, ID_POPUP_TOPMOST, MF_UNCHECKED|MF_BYCOMMAND);
1021                                 }
1022
1023                                 if (servMgr->startWithGui)
1024                                 {
1025                                         CheckMenuItem(trayMenu, ID_POPUP_START_WITH_GUI, MF_CHECKED|MF_BYCOMMAND);
1026                                 } else
1027                                 {
1028                                         CheckMenuItem(trayMenu, ID_POPUP_START_WITH_GUI, MF_UNCHECKED|MF_BYCOMMAND);
1029                                 }
1030
1031                                 SetForegroundWindow(hWnd);    
1032                                 bool skipMenu=false;
1033
1034                                 allowTrayMenu = false;
1035
1036                                 // check for notifications
1037                                 if (currNotify & ServMgr::NT_UPGRADE)
1038                                 {
1039                                         if (servMgr->downloadURL[0])
1040                                         {
1041                                                 if ((sys->getTime()-seenNewVersionTime) > (60*60))      // notify every hour
1042                                                 {
1043                                                         if (MessageBox(hWnd,"A newer version of PeerCast is available, press OK to upgrade.","PeerCast",MB_OKCANCEL|MB_APPLMODAL|MB_ICONEXCLAMATION) == IDOK)
1044                                                                 sys->getURL(servMgr->downloadURL);
1045
1046                                                         seenNewVersionTime=sys->getTime();
1047                                                         skipMenu=true;
1048                                                 }
1049                                         }
1050                                 }
1051
1052
1053                                 if (!skipMenu)
1054                                 {
1055                                         RECT rcWnd;
1056                                         HMENU menu;
1057                                         UINT flg = 0;
1058
1059                                         SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWnd, 0);
1060                                         GetCursorPos(&point);
1061
1062                                         if (point.x < rcWnd.left){
1063                                                 point.x = rcWnd.left;
1064                                                 flg |= TPM_LEFTALIGN;
1065                                         }
1066                                         if (point.x > rcWnd.right){
1067                                                 point.x = rcWnd.right;
1068                                                 flg |= TPM_RIGHTALIGN;
1069                                         }
1070                                         if (point.y < rcWnd.top){
1071                                                 point.y = rcWnd.top;
1072                                                 flg |= TPM_TOPALIGN;
1073                                         }
1074                                         if (point.y > rcWnd.bottom){
1075                                                 point.y = rcWnd.bottom;
1076                                                 flg |= TPM_BOTTOMALIGN;
1077                                         }
1078                                         if (flg == 0){
1079                                                 flg = TPM_RIGHTALIGN;
1080                                         }
1081
1082                                         switch (wParam)
1083                                         {
1084                                                 case 1:
1085                                                         menu = GetSubMenu(trayMenu,0);
1086                                                         addAllChannelsMenu(GetSubMenu(menu,0));
1087                                                         addRelayedChannelsMenu(GetSubMenu(menu,1));
1088                                                         break;
1089                                                 case 2:
1090                                                         menu = GetSubMenu(ltrayMenu,0);
1091                                                         addAllChannelsMenu(menu);
1092                                                         break;
1093                                         }
1094                                         if (!TrackPopupMenu(menu,flg,point.x,point.y,0,hWnd,NULL))
1095                                         {
1096                                                 LOG_ERROR("Can`t track popup menu: %d",GetLastError());
1097                                         }
1098                                         PostMessage(hWnd,WM_NULL,0,0); 
1099
1100                                 }
1101                                 allowTrayMenu = true;
1102                         }
1103                         break;
1104
1105                 case WM_CREATE:
1106                         if (showGUI)
1107                                 createGUI(hWnd);
1108                         break;
1109
1110                 case WM_COMMAND:
1111                         wmId    = LOWORD(wParam); 
1112                         wmEvent = HIWORD(wParam); 
1113
1114                         if ((wmId >= INFO_CMD) && (wmId < INFO_CMD+MAX_CHANNELS))
1115                         {
1116                                 int c = wmId - INFO_CMD;
1117                                 chanInfo = getChannelInfo(c);
1118                                 chanInfoIsRelayed = false;
1119                                 if (winDistinctionNT)
1120                                         DialogBox(hInst, (LPCTSTR)IDD_CHANINFO, hWnd, (DLGPROC)ChanInfoProc);
1121                                 else
1122                                 {
1123                                         HWND WKDLG; //JP-Patch
1124                                         WKDLG = CreateDialog(hInst, (LPCTSTR)IDD_CHANINFO, hWnd, (DLGPROC)ChanInfoProc); //JP-Patch
1125                                         ShowWindow(WKDLG,SW_SHOWNORMAL); //JP-Patch
1126                                 }
1127                                 return 0;
1128                         }
1129                         if ((wmId >= URL_CMD) && (wmId < URL_CMD+MAX_CHANNELS))
1130                         {
1131                                 int c = wmId - URL_CMD;
1132                                 chanInfo = getChannelInfo(c);
1133                                 if (chanInfo.url.isValidURL())
1134                                         sys->getURL(chanInfo.url);
1135                                 return 0;
1136                         }
1137                         if ((wmId >= PLAY_CMD) && (wmId < PLAY_CMD+MAX_CHANNELS))
1138                         {
1139                                 int c = wmId - PLAY_CMD;
1140                                 chanInfo = getChannelInfo(c);
1141                                 chanMgr->findAndPlayChannel(chanInfo,false);
1142                                 return 0;
1143                         }
1144                         if ((wmId >= RELAY_CMD) && (wmId < RELAY_CMD+MAX_CHANNELS))
1145                         {
1146                                 int c = wmId - RELAY_CMD;
1147                                 chanInfo = getChannelInfo(c);
1148                                 chanMgr->findAndPlayChannel(chanInfo,true);
1149                                 return 0;
1150                         }
1151
1152                         // Parse the menu selections:
1153                         switch (wmId)
1154                         {
1155                                 case ID_POPUP_SHOWMESSAGES_PEERCAST:
1156                                         flipNotifyPopup(ID_POPUP_SHOWMESSAGES_PEERCAST,ServMgr::NT_PEERCAST);
1157                                         break;
1158                                 case ID_POPUP_SHOWMESSAGES_BROADCASTERS:
1159                                         flipNotifyPopup(ID_POPUP_SHOWMESSAGES_BROADCASTERS,ServMgr::NT_BROADCASTERS);
1160                                         break;
1161                                 case ID_POPUP_SHOWMESSAGES_TRACKINFO:
1162                                         flipNotifyPopup(ID_POPUP_SHOWMESSAGES_TRACKINFO,ServMgr::NT_TRACKINFO);
1163                                         break;
1164
1165                                 case ID_POPUP_ABOUT:
1166                                 case IDM_ABOUT:
1167                                         DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
1168                                         break;
1169                                 case ID_POPUP_SHOWGUI:
1170                                 case IDM_SETTINGS_GUI:
1171                                 case ID_POPUP_ADVANCED_SHOWGUI:
1172                                 {
1173                                         createGUI(hWnd);
1174                                         break;
1175                                 }
1176                                 case ID_POPUP_YELLOWPAGES:
1177                                         sys->getURL("http://yp.peercast.org/");
1178                                         break;
1179                                 case ID_POPUP_YELLOWPAGES1:
1180                                         sprintf(buf, "http://%s",servMgr->rootHost.cstr());
1181                                         sys->getURL(buf);
1182                                         break;
1183                                 case ID_POPUP_YELLOWPAGES2:
1184                                         sprintf(buf, "http://%s",servMgr->rootHost2.cstr());
1185                                         sys->getURL(buf);
1186                                         break;
1187
1188                                 case ID_POPUP_ADVANCED_VIEWLOG:
1189                                         showHTML("viewlog.html");
1190                                         break;
1191                                 case ID_POPUP_ADVANCED_SAVESETTINGS:
1192                                         servMgr->saveSettings(iniFileName.cstr());
1193                                         break;
1194                                 case ID_POPUP_ADVANCED_INFORMATION:
1195                                         showHTML("index.html");
1196                                         break;
1197                                 case ID_FIND_CHANNELS:
1198                                 case ID_POPUP_ADVANCED_ALLCHANNELS:
1199                                 case ID_POPUP_UPGRADE:
1200                                         sys->callLocalURL("admin?cmd=upgrade",servMgr->serverHost.port);
1201                                         break;
1202                                 case ID_POPUP_ADVANCED_RELAYEDCHANNELS:
1203                                 case ID_POPUP_FAVORITES_EDIT:
1204                                         showHTML("relays.html");
1205                                         break;
1206                                 case ID_POPUP_ADVANCED_BROADCAST:
1207                                         showHTML("broadcast.html");
1208                                         break;
1209                                 case ID_POPUP_SETTINGS:
1210                                         showHTML("settings.html");
1211                                         break;
1212                                 case ID_POPUP_CONNECTIONS:
1213                                         showHTML("connections.html");
1214                                         break;
1215                                 case ID_POPUP_HELP:
1216                                         sys->getURL("http://www.peercast.org/help.php");
1217                                         break;
1218
1219                                 case ID_POPUP_SAVE_GUI_POS:
1220                                         if (servMgr->saveGuiPos){
1221                                                 servMgr->saveGuiPos = false;
1222                                                 CheckMenuItem(trayMenu, ID_POPUP_SAVE_GUI_POS, MF_UNCHECKED|MF_BYCOMMAND);
1223                                         } else {
1224                                                 servMgr->saveGuiPos = true;
1225                                                 CheckMenuItem(trayMenu, ID_POPUP_SAVE_GUI_POS, MF_CHECKED|MF_BYCOMMAND);
1226                                         }
1227                                         peercastInst->saveSettings();
1228                                         break;
1229
1230                                 case ID_POPUP_KEEP_DOWNSTREAMS:
1231                                         if (servMgr->keepDownstreams){
1232                                                 servMgr->keepDownstreams = false;
1233                                                 CheckMenuItem(trayMenu, ID_POPUP_KEEP_DOWNSTREAMS, MF_UNCHECKED|MF_BYCOMMAND);
1234                                         } else {
1235                                                 servMgr->keepDownstreams = true;
1236                                                 CheckMenuItem(trayMenu, ID_POPUP_KEEP_DOWNSTREAMS, MF_CHECKED|MF_BYCOMMAND);
1237                                         }
1238                                         //peercastInst->saveSettings();
1239                                         break;
1240
1241                                 case ID_POPUP_TOPMOST:
1242                                         // \8dÅ\91O\96Ê\95\\8e¦
1243                                         if (servMgr->topmostGui)
1244                                         {
1245                                                 servMgr->topmostGui = false;
1246                                                 CheckMenuItem(trayMenu, ID_POPUP_TOPMOST, MF_UNCHECKED|MF_BYCOMMAND);
1247                                         } else
1248                                         {
1249                                                 servMgr->topmostGui = true;
1250                                                 CheckMenuItem(trayMenu, ID_POPUP_TOPMOST, MF_CHECKED|MF_BYCOMMAND);
1251                                         }
1252                                         peercastInst->saveSettings();
1253                                         break;
1254
1255                                 case ID_POPUP_START_WITH_GUI:
1256                                         // \8bN\93®\8e\9e\82ÉGUI\95\\8e¦
1257                                         if (servMgr->startWithGui)
1258                                         {
1259                                                 servMgr->startWithGui = false;
1260                                                 CheckMenuItem(trayMenu, ID_POPUP_START_WITH_GUI, MF_UNCHECKED|MF_BYCOMMAND);
1261                                         } else
1262                                         {
1263                                                 servMgr->startWithGui = true;
1264                                                 CheckMenuItem(trayMenu, ID_POPUP_START_WITH_GUI, MF_CHECKED|MF_BYCOMMAND);
1265                                         }
1266                                         peercastInst->saveSettings();
1267                                         break;
1268
1269                                 case ID_POPUP_EXIT_CONFIRM:
1270                                 case IDM_EXIT:
1271                                    DestroyWindow(hWnd);
1272                                    break;
1273                                 default:
1274                                    return DefWindowProc(hWnd, message, wParam, lParam);
1275                         }
1276                         break;
1277                 case WM_DESTROY:
1278                         PostQuitMessage(0);
1279                         break;
1280                 default:
1281                         return DefWindowProc(hWnd, message, wParam, lParam);
1282    }
1283    return 0;
1284 }
1285 // Mesage handler for about box.
1286 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1287 {
1288         switch (message)
1289         {
1290                 case WM_INITDIALOG:
1291                         //SendDlgItemMessage(hDlg,IDC_ABOUTVER,WM_SETTEXT,0,(LONG)PCX_AGENT);
1292 //                      SendDlgItemMessage(hDlg,IDC_ABOUTVER,WM_SETTEXT,0,(LONG)PCX_AGENTJP);
1293 #ifdef VERSION_EX
1294                         SendDlgItemMessage(hDlg,IDC_ABOUTVER,WM_SETTEXT,0,(LONG)PCX_AGENTEX);
1295 #else
1296                         SendDlgItemMessage(hDlg,IDC_ABOUTVER,WM_SETTEXT,0,(LONG)PCX_AGENTVP);
1297 #endif
1298                         return TRUE;
1299
1300                 case WM_COMMAND:
1301                         switch (LOWORD(wParam))
1302                         {
1303                                 case IDOK:
1304                                 case IDCANCEL:
1305                                         EndDialog(hDlg, LOWORD(wParam));
1306                                         return TRUE;
1307                                 case IDC_BUTTON1:
1308                                         sys->getURL("http://www.peercast.org");
1309                                         EndDialog(hDlg, LOWORD(wParam));
1310                                         return TRUE;
1311
1312                         }
1313                         break;
1314                 case WM_DESTROY:
1315                         break;
1316         }
1317     return FALSE;
1318 }
1319
1320 // Mesage handler for chaninfo box
1321 LRESULT CALLBACK ChanInfoProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1322 {
1323         switch (message)
1324         {
1325                 case WM_INITDIALOG:
1326                         {
1327                                 char str[1024];
1328                                 //strcpy(str,chanInfo.track.artist.cstr());
1329                                 strcpy(str,chanInfo.track.artist); //JP-Patch
1330                                 strcat(str," - ");
1331                                 //strcat(str,chanInfo.track.title.cstr());
1332                                 strcat(str,chanInfo.track.title);
1333                                 String name,track,comment,desc,genre; //JP-Patch
1334                                 name = chanInfo.name; //JP-Patch
1335                                 track = str; //JP-Patch
1336                                 comment = chanInfo.comment; //JP-Patch
1337                                 desc = chanInfo.desc; //JP-Patc
1338                                 genre = chanInfo.genre; //JP-Patch
1339                                 name.convertTo(String::T_SJIS); //JP-Patc
1340                                 track.convertTo(String::T_SJIS); //JP-Patch
1341                                 comment.convertTo(String::T_SJIS); //JP-Patch
1342                                 desc.convertTo(String::T_SJIS); //JP-Patch
1343                                 genre.convertTo(String::T_SJIS); //JP-Patch
1344                                 
1345                                 //SendDlgItemMessage(hDlg,IDC_EDIT_NAME,WM_SETTEXT,0,(LONG)chanInfo.name.cstr());
1346                                 SendDlgItemMessage(hDlg,IDC_EDIT_NAME,WM_SETTEXT,0,(LONG)name.cstr()); //JP-Patch
1347                                 //SendDlgItemMessage(hDlg,IDC_EDIT_PLAYING,WM_SETTEXT,0,(LONG)str);
1348                                 SendDlgItemMessage(hDlg,IDC_EDIT_PLAYING,WM_SETTEXT,0,(LONG)track.cstr()); //JP-Patch
1349                                 //SendDlgItemMessage(hDlg,IDC_EDIT_MESSAGE,WM_SETTEXT,0,(LONG)chanInfo.comment.cstr());
1350                                 SendDlgItemMessage(hDlg,IDC_EDIT_MESSAGE,WM_SETTEXT,0,(LONG)comment.cstr()); //JP-Patch
1351                                 //SendDlgItemMessage(hDlg,IDC_EDIT_DESC,WM_SETTEXT,0,(LONG)chanInfo.desc.cstr());
1352                                 SendDlgItemMessage(hDlg,IDC_EDIT_DESC,WM_SETTEXT,0,(LONG)desc.cstr()); //JP-Patch
1353                                 //SendDlgItemMessage(hDlg,IDC_EDIT_GENRE,WM_SETTEXT,0,(LONG)chanInfo.genre.cstr());
1354                                 SendDlgItemMessage(hDlg,IDC_EDIT_GENRE,WM_SETTEXT,0,(LONG)genre.cstr()); //JP-Patch
1355
1356                                 sprintf(str,"%d kb/s %s",chanInfo.bitrate,ChanInfo::getTypeStr(chanInfo.contentType));
1357                                 SendDlgItemMessage(hDlg,IDC_FORMAT,WM_SETTEXT,0,(LONG)str);
1358
1359
1360                                 if (!chanInfo.url.isValidURL())
1361                                         EnableWindow(GetDlgItem(hDlg,IDC_CONTACT),false);
1362
1363                                 Channel *ch = chanMgr->findChannelByID(chanInfo.id);
1364                                 if (ch)
1365                                 {
1366                                         SendDlgItemMessage(hDlg,IDC_EDIT_STATUS,WM_SETTEXT,0,(LONG)ch->getStatusStr());
1367                                         SendDlgItemMessage(hDlg, IDC_KEEP,BM_SETCHECK, ch->stayConnected, 0);
1368
1369                                         // \8c»\8dÝ\82Ì\8cÅ\97L\83\8a\83\8c\81[\8fã\8cÀ\90Ý\92è\82ð\95\\8e¦(0\82Í\96³\8cø)
1370                                         ::SetDlgItemInt(hDlg, IDC_EDIT_MAXRELAYS, ch->maxRelays, false);
1371                                         if (isIndexTxt(ch))
1372                                         {
1373                                                 // index.txt\82È\82Ì\82Å\96³\8cø\82É
1374                                                 ::EnableWindow(::GetDlgItem(hDlg, IDC_EDIT_MAXRELAYS), false);
1375                                                 ::EnableWindow(::GetDlgItem(hDlg, IDC_APPLY_MAXRELAYS), false);
1376                                         }
1377                                 }else
1378                                 {
1379                                         SendDlgItemMessage(hDlg,IDC_EDIT_STATUS,WM_SETTEXT,0,(LONG)"OK");
1380                                         EnableWindow(GetDlgItem(hDlg,IDC_KEEP),false);
1381                                 }
1382
1383
1384
1385                                 POINT point;
1386                                 RECT rect,drect;
1387                                 HWND hDsk = GetDesktopWindow();
1388                                 GetWindowRect(hDsk,&drect);
1389                                 GetWindowRect(hDlg,&rect);
1390                                 GetCursorPos(&point);
1391
1392                                 POINT pos,size;
1393                                 size.x = rect.right-rect.left;
1394                                 size.y = rect.bottom-rect.top;
1395
1396                                 if (point.x-drect.left < size.x)
1397                                         pos.x = point.x;
1398                                 else
1399                                         pos.x = point.x-size.x;
1400
1401                                 if (point.y-drect.top < size.y)
1402                                         pos.y = point.y;
1403                                 else
1404                                         pos.y = point.y-size.y;
1405
1406                                 SetWindowPos(hDlg,HWND_TOPMOST,pos.x,pos.y,size.x,size.y,0);
1407                                 chWnd = hDlg;
1408                         }
1409                         return TRUE;
1410
1411                 case WM_COMMAND:
1412                         {
1413                                 char str[1024],idstr[64];
1414                                 chanInfo.id.toStr(idstr);
1415
1416                                 switch (LOWORD(wParam))
1417                                 {
1418                                 case IDC_CONTACT:
1419                                         {
1420                                                 sys->getURL(chanInfo.url);
1421                                                 return TRUE;
1422                                         }
1423                                 case IDC_DETAILS:
1424                                         {
1425                                                 sprintf(str,"admin?page=chaninfo&id=%s&relay=%d",idstr,chanInfoIsRelayed);
1426                                                 sys->callLocalURL(str,servMgr->serverHost.port);
1427                                                 return TRUE;
1428                                         }
1429                                 case IDC_KEEP:
1430                                         {
1431                                                 Channel *ch = chanMgr->findChannelByID(chanInfo.id);
1432                                                 if (ch)
1433                                                         ch->stayConnected = SendDlgItemMessage(hDlg, IDC_KEEP,BM_GETCHECK, 0, 0) == BST_CHECKED;;
1434                                                 return TRUE;
1435                                         }
1436
1437
1438                                 case IDC_PLAY:
1439                                         {
1440                                                 chanMgr->findAndPlayChannel(chanInfo,false);
1441                                                 return TRUE;
1442                                         }
1443
1444                                 case IDC_APPLY_MAXRELAYS:
1445                                         {
1446                                                 // \83`\83\83\83\93\83l\83\8b\8cÅ\97L\82Ì\8dÅ\91å\83\8a\83\8c\81[\90\94\82ð\90Ý\92è
1447                                                 BOOL bSucc;
1448                                                 unsigned int mr;
1449
1450                                                 // \93ü\97Í\92l\8eæ\93¾
1451                                                 mr = ::GetDlgItemInt(hDlg, IDC_EDIT_MAXRELAYS, &bSucc, false);
1452
1453                                                 if (bSucc)
1454                                                 {
1455                                                         Channel *ch = chanMgr->findChannelByID(chanInfo.id);
1456                                                         if (ch && !isIndexTxt(ch))
1457                                                         {
1458                                                                 ch->maxRelays = mr;
1459                                                         }
1460                                                 } else
1461                                                 {
1462                                                         MessageBox(hDlg, "\93ü\97Í\92l\82ª\95s\90³\82Å\82·\81B", "Error", MB_OK|MB_ICONERROR|MB_APPLMODAL);
1463                                                         Channel *ch = chanMgr->findChannelByID(chanInfo.id);
1464                                                         if (ch)
1465                                                                 ::SetDlgItemInt(hDlg, IDC_EDIT_MAXRELAYS, ch->maxRelays, false);
1466                                                 }
1467                                         }
1468                                 }
1469                         }
1470                         break;
1471
1472                 case WM_CLOSE:
1473                         if (winDistinctionNT)
1474                                 EndDialog(hDlg, 0);
1475                         else
1476                                 DestroyWindow(hDlg); //JP-Patch
1477                         break;
1478
1479                 case WM_ACTIVATE:
1480                         if (LOWORD(wParam) == WA_INACTIVE)
1481                                 if (winDistinctionNT)
1482                                         EndDialog(hDlg, 0);
1483                                 else
1484                                         DestroyWindow(hDlg); //JP-Patch
1485                         break;
1486                 case WM_DESTROY:
1487                         chWnd = NULL;
1488                         break;
1489
1490
1491         }
1492     return FALSE;
1493 }