OSDN Git Service

replace 'SUCCESS' and 'FAIL' macro to 'FFFTP_SUCCESS' and 'FFFTP_FAIL' to suppress...
[ffftp/ffftp.git] / history.c
1 /*=============================================================================\r
2 *\r
3 *                                                                               ヒストリ\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 #define  STRICT\r
31 #include <windows.h>\r
32 #include <stdio.h>\r
33 #include <stdlib.h>\r
34 #include <string.h>\r
35 #include <mbstring.h>\r
36 #include <malloc.h>\r
37 #include <windowsx.h>\r
38 #include <commctrl.h>\r
39 #include <stdarg.h>\r
40 #include <winsock.h>\r
41 \r
42 #include "common.h"\r
43 #include "resource.h"\r
44 \r
45 \r
46 /*===== プロトタイプ =====*/\r
47 \r
48 static void CopyHostToHistory(HOSTDATA *Host, HISTORYDATA *New);\r
49 static void AddOneFnameToMenu(char *Host, char *User, char *Remote, int Num);\r
50 static void RemoveAllHistoryFromMenu(void);\r
51 \r
52 /*===== 外部参照 =====*/\r
53 \r
54 /* 設定値 */\r
55 extern int FileHist;\r
56 extern int PassToHist;\r
57 \r
58 /*===== ローカルなワーク =====*/\r
59 \r
60 static HISTORYDATA *HistoryBase = NULL;\r
61 static int HistoryNum = 0;\r
62 \r
63 /* ヒストリのメニュー項目のID */\r
64 static int MenuHistId[HISTORY_MAX] = {\r
65         MENU_HIST_1,  MENU_HIST_2,  MENU_HIST_3,  MENU_HIST_4,  MENU_HIST_5,\r
66         MENU_HIST_6,  MENU_HIST_7,  MENU_HIST_8,  MENU_HIST_9,  MENU_HIST_10,\r
67         MENU_HIST_11, MENU_HIST_12, MENU_HIST_13, MENU_HIST_14, MENU_HIST_15,\r
68         MENU_HIST_16, MENU_HIST_17, MENU_HIST_18, MENU_HIST_19, MENU_HIST_20\r
69 };\r
70 \r
71 \r
72 \r
73 /*----- ホスト情報をヒストリリストの先頭に追加する ----------------------------\r
74 *\r
75 *       Parameter\r
76 *               HOSTDATA *Host : ホストデータ\r
77 *               int TrMode : 転送モード\r
78 *\r
79 *       Return Value\r
80 *               なし\r
81 *----------------------------------------------------------------------------*/\r
82 \r
83 void AddHostToHistory(HOSTDATA *Host, int TrMode)\r
84 {\r
85         HISTORYDATA New;\r
86 \r
87         CopyHostToHistory(Host, &New);\r
88         New.Type = TrMode;\r
89         AddHistoryToHistory(&New);\r
90         return;\r
91 }\r
92 \r
93 \r
94 /*----- ヒストリをヒストリリストの先頭に追加する ------------------------------\r
95 *\r
96 *       Parameter\r
97 *               HISTORYDATA *Hist : ヒストリデータ\r
98 *\r
99 *       Return Value\r
100 *               なし\r
101 *----------------------------------------------------------------------------*/\r
102 \r
103 void AddHistoryToHistory(HISTORYDATA *Hist)\r
104 {\r
105         HISTORYDATA *New;\r
106 \r
107         CheckHistoryNum(1);\r
108         if(FileHist > HistoryNum)\r
109         {\r
110                 New = malloc(sizeof(HISTORYDATA));\r
111                 if(New != NULL)\r
112                 {\r
113                         memcpy(New, Hist, sizeof(HISTORYDATA));\r
114                         New->Next = HistoryBase;\r
115                         HistoryBase = New;\r
116                         HistoryNum++;\r
117                 }\r
118         }\r
119         return;\r
120 }\r
121 \r
122 \r
123 /*----- ヒストリの数を返す ----------------------------------------------------\r
124 *\r
125 *       Parameter\r
126 *               なし\r
127 *\r
128 *       Return Value\r
129 *               int ヒストリの数\r
130 *----------------------------------------------------------------------------*/\r
131 \r
132 int AskHistoryNum(void)\r
133 {\r
134         return(HistoryNum);\r
135 }\r
136 \r
137 \r
138 /*----- ヒストリの数をチェックし多すぎたら削除 --------------------------------\r
139 *\r
140 *       Parameter\r
141 *               int Space : 空けておく個数 (0~)\r
142 *\r
143 *       Return Value\r
144 *               なし\r
145 *----------------------------------------------------------------------------*/\r
146 \r
147 void CheckHistoryNum(int Space)\r
148 {\r
149         int i;\r
150         HISTORYDATA *Prev;\r
151         HISTORYDATA *Pos;\r
152         HISTORYDATA *Next;\r
153 \r
154         if(HistoryNum > FileHist-Space)\r
155         {\r
156                 /* 残すべきヒストリを探す */\r
157                 Pos = HistoryBase;\r
158                 Prev = NULL;\r
159                 for(i = 0; i < FileHist-Space; i++)\r
160                 {\r
161                         Prev = Pos;\r
162                         Pos = Pos->Next;\r
163                 }\r
164 \r
165                 /* いらないヒストリを消す */\r
166                 if(Prev == NULL)\r
167                         HistoryBase = NULL;\r
168                 else\r
169                         Prev->Next = NULL;\r
170 \r
171                 while(Pos != NULL)\r
172                 {\r
173                         Next = Pos->Next;\r
174                         free(Pos);\r
175                         Pos = Next;\r
176                         HistoryNum--;\r
177                 }\r
178         }\r
179         return;\r
180 }\r
181 \r
182 \r
183 /*----- ホスト情報をヒストリにセット ------------------------------------------\r
184 *\r
185 *       Parameter\r
186 *               HOSTDATA *Host : ホストデータ\r
187 *               HISTORYDATA *New : ヒストリをセットするワーク\r
188 *\r
189 *       Return Value\r
190 *               なし\r
191 *----------------------------------------------------------------------------*/\r
192 \r
193 static void CopyHostToHistory(HOSTDATA *Host, HISTORYDATA *New)\r
194 {\r
195         strcpy(New->HostAdrs, Host->HostAdrs);\r
196         strcpy(New->UserName, Host->UserName);\r
197         if(PassToHist == YES)\r
198                 strcpy(New->PassWord, Host->PassWord);\r
199         else\r
200                 strcpy(New->PassWord, "");\r
201         strcpy(New->Account, Host->Account);\r
202         strcpy(New->LocalInitDir, Host->LocalInitDir);\r
203         strcpy(New->RemoteInitDir, Host->RemoteInitDir);\r
204         strcpy(New->ChmodCmd, Host->ChmodCmd);\r
205         strcpy(New->LsName, Host->LsName);\r
206         strcpy(New->InitCmd, Host->InitCmd);\r
207         New->Port = Host->Port;\r
208         New->KanjiCode = Host->KanjiCode;\r
209         New->KanaCnv = Host->KanaCnv;\r
210         New->NameKanjiCode = Host->NameKanjiCode;\r
211         New->NameKanaCnv = Host->NameKanaCnv;\r
212         New->Pasv = Host->Pasv;\r
213         New->FireWall = Host->FireWall;\r
214         New->ListCmdOnly = Host->ListCmdOnly;\r
215         New->UseNLST_R = Host->UseNLST_R;\r
216         New->TimeZone = Host->TimeZone;\r
217         New->HostType = Host->HostType;\r
218         New->SyncMove = Host->SyncMove;\r
219         New->NoFullPath = Host->NoFullPath;\r
220         New->Sort = Host->Sort;\r
221         New->Security = Host->Security;\r
222         New->Dialup = Host->Dialup;\r
223         New->DialupAlways = Host->DialupAlways;\r
224         New->DialupNotify = Host->DialupNotify;\r
225         strcpy(New->DialEntry, Host->DialEntry);\r
226         return;\r
227 }\r
228 \r
229 \r
230 /*----- ヒストリ情報をホスト情報にセット --------------------------------------\r
231 *\r
232 *       Parameter\r
233 *               HISTORYDATA *Hist : ヒストリ\r
234 *               HOSTDATA *Host : ホストデータをセットするワーク\r
235 *\r
236 *       Return Value\r
237 *               なし\r
238 *----------------------------------------------------------------------------*/\r
239 \r
240 void CopyHistoryToHost(HISTORYDATA *Hist, HOSTDATA *Host)\r
241 {\r
242         CopyDefaultHost(Host);\r
243 \r
244         strcpy(Host->HostAdrs, Hist->HostAdrs);\r
245         strcpy(Host->UserName, Hist->UserName);\r
246         if(PassToHist == YES)\r
247                 strcpy(Host->PassWord, Hist->PassWord);\r
248         else\r
249                 strcpy(Host->PassWord, "");\r
250         strcpy(Host->Account, Hist->Account);\r
251         strcpy(Host->LocalInitDir, Hist->LocalInitDir);\r
252         strcpy(Host->RemoteInitDir, Hist->RemoteInitDir);\r
253         strcpy(Host->ChmodCmd, Hist->ChmodCmd);\r
254         strcpy(Host->LsName, Hist->LsName);\r
255         strcpy(Host->InitCmd, Hist->InitCmd);\r
256         Host->Port = Hist->Port;\r
257         Host->KanjiCode = Hist->KanjiCode;\r
258         Host->KanaCnv = Hist->KanaCnv;\r
259         Host->NameKanjiCode = Hist->NameKanjiCode;\r
260         Host->NameKanaCnv = Hist->NameKanaCnv;\r
261         Host->Pasv = Hist->Pasv;\r
262         Host->FireWall = Hist->FireWall;\r
263         Host->ListCmdOnly = Hist->ListCmdOnly;\r
264         Host->UseNLST_R = Hist->UseNLST_R;\r
265         Host->TimeZone = Hist->TimeZone;\r
266         Host->HostType = Hist->HostType;\r
267         Host->SyncMove = Hist->SyncMove;\r
268         Host->NoFullPath = Hist->NoFullPath;\r
269         Host->Sort = Hist->Sort;\r
270         Host->Security = Hist->Security;\r
271         Host->Dialup = Hist->Dialup;\r
272         Host->DialupAlways = Hist->DialupAlways;\r
273         Host->DialupNotify = Hist->DialupNotify;\r
274         strcpy(Host->DialEntry, Hist->DialEntry);\r
275         return;\r
276 }\r
277 \r
278 \r
279 /*----- ヒストリ情報の初期値を取得 --------------------------------------------\r
280 *\r
281 *       Parameter\r
282 *               HISTORYDATA *Set : ヒストリをセットするワーク\r
283 *\r
284 *       Return Value\r
285 *               なし\r
286 *----------------------------------------------------------------------------*/\r
287 \r
288 void CopyDefaultHistory(HISTORYDATA *Set)\r
289 {\r
290         HOSTDATA Host;\r
291 \r
292         CopyDefaultHost(&Host);\r
293         CopyHostToHistory(&Host, Set);\r
294         return;\r
295 }\r
296 \r
297 \r
298 /*----- 全ヒストリをメニューにセット ------------------------------------------\r
299 *\r
300 *       Parameter\r
301 *               なし\r
302 *\r
303 *       Return Value\r
304 *               なし\r
305 *----------------------------------------------------------------------------*/\r
306 \r
307 void SetAllHistoryToMenu(void)\r
308 {\r
309         int i;\r
310         HISTORYDATA *Pos;\r
311 \r
312         RemoveAllHistoryFromMenu();\r
313 \r
314         Pos = HistoryBase;\r
315         for(i = 0; i < HistoryNum; i++)\r
316         {\r
317                 AddOneFnameToMenu(Pos->HostAdrs, Pos->UserName, Pos->RemoteInitDir, i);\r
318                 Pos = Pos->Next;\r
319         }\r
320         return;\r
321 }\r
322 \r
323 \r
324 /*----- ヒストリをメニューに追加 ----------------------------------------------\r
325 *\r
326 *       Parameter\r
327 *               char *Host : ホスト名\r
328 *               char *User : ユーザ名\r
329 *               char *Remote : ホストのフォルダ\r
330 *               int Num : 番号\r
331 *\r
332 *       Return Value\r
333 *               なし\r
334 *----------------------------------------------------------------------------*/\r
335 \r
336 static void AddOneFnameToMenu(char *Host, char *User, char *Remote, int Num)\r
337 {\r
338         HMENU hMenu;\r
339         char Tmp[HOST_ADRS_LEN+USER_NAME_LEN+INIT_DIR_LEN+7+1];\r
340 \r
341         hMenu = GetSubMenu(GetMenu(GetMainHwnd()), 0);\r
342 \r
343         if(Num == 0)\r
344                 AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);\r
345 \r
346         if(Num < 9)\r
347                 sprintf(Tmp, "&%d %s (%s) %s", Num+1, Host, User, Remote);\r
348         else if(Num == 9)\r
349                 sprintf(Tmp, "&0 %s (%s) %s", Host, User, Remote);\r
350         else\r
351                 sprintf(Tmp, "&* %s (%s) %s", Host, User, Remote);\r
352 \r
353         AppendMenu(hMenu, MF_STRING, MenuHistId[Num], Tmp);\r
354 \r
355         return;\r
356 }\r
357 \r
358 \r
359 /*----- 全ヒストリをメニューから削除 ------------------------------------------\r
360 *\r
361 *       Parameter\r
362 *               なし\r
363 *\r
364 *       Return Value\r
365 *               なし\r
366 *----------------------------------------------------------------------------*/\r
367 \r
368 static void RemoveAllHistoryFromMenu(void)\r
369 {\r
370         HMENU hMenu;\r
371         int Cnt;\r
372         int i;\r
373 \r
374         hMenu = GetSubMenu(GetMenu(GetMainHwnd()), 0);\r
375         Cnt = GetMenuItemCount(hMenu);\r
376         for(i = DEF_FMENU_ITEMS; i < Cnt; i++)\r
377         {\r
378                 DeleteMenu(hMenu, DEF_FMENU_ITEMS, MF_BYPOSITION);\r
379         }\r
380         return;\r
381 }\r
382 \r
383 \r
384 /*----- 指定メニューコマンドに対応するヒストリを返す --------------------------\r
385 *\r
386 *       Parameter\r
387 *               int MenuCmd : 取り出すヒストリに割り当てられたメニューコマンド (MENU_xxx)\r
388 *               HISTORYDATA *Buf : ヒストリデータを返すバッファ\r
389 *\r
390 *       Return Value\r
391 *               int ステータス\r
392 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
393 *----------------------------------------------------------------------------*/\r
394 \r
395 int GetHistoryByCmd(int MenuCmd, HISTORYDATA *Buf)\r
396 {\r
397         int Sts;\r
398         int i;\r
399         HISTORYDATA *Pos;\r
400 \r
401         Sts = FFFTP_FAIL;\r
402         Pos = HistoryBase;\r
403         for(i = 0; i < HistoryNum; i++)\r
404         {\r
405                 if(MenuHistId[i] == MenuCmd)\r
406                 {\r
407                         memcpy(Buf, Pos, sizeof(HISTORYDATA));\r
408                         Sts = FFFTP_SUCCESS;\r
409                 }\r
410                 Pos = Pos->Next;\r
411         }\r
412         return(Sts);\r
413 }\r
414 \r
415 \r
416 /*----- 指定番号に対応するヒストリを返す --------------------------------------\r
417 *\r
418 *       Parameter\r
419 *               int Num : 番号(0~)\r
420 *               HISTORYDATA *Buf : ヒストリデータを返すバッファ\r
421 *\r
422 *       Return Value\r
423 *               int ステータス\r
424 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
425 *----------------------------------------------------------------------------*/\r
426 \r
427 int GetHistoryByNum(int Num, HISTORYDATA *Buf)\r
428 {\r
429         return(GetHistoryByCmd(MenuHistId[Num], Buf));\r
430 }\r
431 \r
432 \r