OSDN Git Service

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