6 Messages, SysUtils, Classes, Contnrs, ComCtrls, {HttpApp,} YofUtils,
7 GikoSystem{, XMLIntf, XMLDoc}, GikoXMLDoc, BoardGroup, windows,
9 {SAX, SAXHelpers, SAXComps, SAXKW;}
12 TFavoriteFolder = class
15 TFavoriteBoardItem = class
20 function GetItem : TBoard;
22 constructor Create( inURL : string; inTitle : string = ''; inItem : TBoard = nil );
23 constructor CreateWithItem( inItem : TBoard );
25 property Item : TBoard read GetItem write FItem;
26 property URL : string read FURL write FURL; // Item
\82ª
\8eæ
\93¾
\82Å
\82«
\82È
\82
\82Ä
\82à URL
\82Í
\8fí
\82É
\95Û
\8e\9d\82³
\82ê
\82é
27 property Title : string read FTitle write FTitle;
30 TFavoriteThreadItem = class
35 function GetItem : TThreadItem;
37 constructor Create( inURL : string; inTitle : string = ''; inItem : TThreadItem = nil );
38 constructor CreateWithItem( inItem : TThreadItem );
40 property Item : TThreadItem read GetItem write FItem;
41 property URL : string read FURL write FURL; // Item
\82ª
\8eæ
\93¾
\82Å
\82«
\82È
\82
\82Ä
\82à URL
\82Í
\8fí
\82É
\95Û
\8e\9d\82³
\82ê
\82é
42 property Title : string read FTitle write FTitle;
45 TFavoriteDM = class(TDataModule)
46 procedure DataModuleDestroy(Sender: TObject);
52 procedure ReadNode(Node: IXMLNode);
53 procedure AddSaveString(Node: TTreeNode; SaveList: TStringList);
54 // procedure AddSaveString(Node: TTreeNode; XMLNode: IXMLNode);
55 // procedure SAXStartDocument(Sender: TObject);
56 // procedure SAXEndDocument(Sender: TObject);
57 // procedure SAXStartElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString; const Atts: IAttributes);
58 // procedure SAXEndElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString);
59 // procedure SAXCharacters(Sender: TObject; const PCh: SAXString);
62 function GetFavoriteFilePath() : String;
63 function SaveFavoriteFile(FileName: String) : Boolean;
64 procedure SetFavTreeView(TreeView: TTreeView);
65 procedure ReadFavorite;
66 procedure WriteFavorite;
67 procedure URLReplace(oldURLs: TStringList; newURLs: TStringList);
68 property TreeView: TTreeView read FTreeView;
72 FavoriteDM: TFavoriteDM;
74 FAVORITE_LINK_NAME = '
\83\8a\83\93\83N';
78 uses ExternalBoardManager, ExternalBoardPlugInMain;
81 FAVORITE_ROOT_NAME = '
\82¨
\8bC
\82É
\93ü
\82è';
82 FAVORITE_FILE_NAME = 'Favorite.xml';
86 constructor TFavoriteBoardItem.Create(
88 inTitle : string = '';
101 constructor TFavoriteBoardItem.CreateWithItem(
106 Create( inItem.URL, inItem.Title, inItem );
109 destructor TFavoriteBoardItem.Destory;
115 function TFavoriteBoardItem.GetItem : TBoard;
117 // category : TCategory;
120 if FItem = nil then begin
121 FItem := BBSsFindBoardFromURL( URL );
123 if FItem = nil then begin
124 //
\81¦
\8dì
\82Á
\82Ä
\82à
\81A
\92Ç
\89Á
\82·
\82é
\83J
\83e
\83S
\83\8a\82ª
\96³
\82¢
\82Ì
\82Å
\8c\83\82µ
\82
\95Û
\97¯
125 FItem := GikoSys.GetUnknownBoard( nil, URL );
126 FItem.Title := Title;
135 constructor TFavoriteThreadItem.Create(
137 inTitle : string = '';
138 inItem : TThreadItem = nil
150 constructor TFavoriteThreadItem.CreateWithItem(
155 Create( inItem.URL, inItem.Title, inItem );
158 destructor TFavoriteThreadItem.Destory;
165 function TFavoriteThreadItem.GetItem : TThreadItem;
169 browsableURL : string;
172 if FItem = nil then begin
173 browsableURL := GikoSys.GetBrowsableThreadURL( URL );
174 boardURL := GikoSys.GetThreadURL2BoardURL( browsableURL );
175 board := BBSsFindBoardFromURL( boardURL );
177 if board = nil then begin
180 //
\81¦
\8dì
\82Á
\82Ä
\82à
\81A
\92Ç
\89Á
\82·
\82é
\83J
\83e
\83S
\83\8a\82ª
\96³
\82¢
\82Ì
\82Å
\8c\83\82µ
\82
\95Û
\97¯
181 //board := GikoSys.GetUnknownBoard( nil, boardURL )
183 FItem := board.FindThreadFromURL( browsableURL );
185 if FItem = nil then begin
186 FItem := TThreadItem.Create( board.BoardPlugIn, browsableURL );
188 FItem.Title := Title;
197 procedure TFavoriteDM.DataModuleDestroy(Sender: TObject);
201 TreeView.Items.BeginUpdate;
202 for i := TreeView.Items.Count - 1 downto 0 do begin
203 TObject(TreeView.Items[i].Data).Free;
205 TreeView.Items.Clear;
206 TreeView.Items.EndUpdate;
210 procedure TFavoriteDM.SetFavTreeView(TreeView: TTreeView);
212 FTreeView := TreeView;
215 procedure TFavoriteDM.ReadFavorite;
218 XMLDoc: IXMLDocument;
222 FavFolder: TFavoriteFolder;
226 FileName := GikoSys.GetConfigDir + FAVORITE_FILE_NAME;
228 FavFolder := TFavoriteFolder.Create;
229 Node := FTreeView.Items.AddChildObject(nil, FAVORITE_ROOT_NAME, FavFolder);
230 Node.ImageIndex := 14;
231 Node.SelectedIndex := 14;
233 if FileExists(FileName) then begin
235 XMLDoc := IXMLDocument.Create;
236 //XMLDoc := LoadXMLDocument(FileName);
237 LoadXMLDocument(FileName, XMLDoc);
238 XMLNode := XMLDoc.DocumentElement;
240 FStack := TStack.Create;
244 if XMLNode.NodeName = 'favorite' then begin
245 for i := 0 to XMLNode.ChildNodes.Count - 1 do begin
246 ReadNode(XMLNode.ChildNodes[i]);
247 if (XMLNode.ChildNodes[i].NodeName = 'folder') and
248 (XMLNode.ChildNodes[i].Attributes['title'] = FAVORITE_LINK_NAME) then begin
253 if not LinkExists then begin
254 FavFolder := TFavoriteFolder.Create;
255 Node := FTreeView.Items.AddChildObject(Node, FAVORITE_LINK_NAME, FavFolder);
256 Node.ImageIndex := 14;
257 Node.SelectedIndex := 14;
268 FavFolder := TFavoriteFolder.Create;
269 Node := FTreeView.Items.AddChildObject(nil, FAVORITE_ROOT_NAME, FavFolder);
270 Node.ImageIndex := 12;
271 Node.SelectedIndex := 13;
273 FileName := GikoSys.GetConfigDir + FAVORITE_FILE_NAME;
274 if not FileExists(FileName) then
277 FavSAXHandler.OnStartDocument := SAXStartDocument;
278 FavSAXHandler.OnEndDocument := SAXEndDocument;
279 FavSAXHandler.OnStartElement := SAXStartElement;
280 FavSAXHandler.OnStartElement := SAXStartElement;
281 FavSAXHandler.OnEndElement := SAXEndElement;
282 FavSAXHandler.OnCharacters := SAXCharacters;
284 FavSAXReader.Vendor := 'Keith Wood';
285 FavSAXReader.URL := FileName;
289 procedure TFavoriteDM.ReadNode(Node: IXMLNode);
293 ParentNode: TTreeNode;
294 CurrentNode: TTreeNode;
295 FavFolder: TFavoriteFolder;
296 FavBoard: TFavoriteBoardItem;
297 FavThread: TFavoriteThreadItem;
299 threadItem : TThreadItem;
301 if Node.NodeName = 'folder' then begin
302 ParentNode := FStack.Peek;
303 if TObject(ParentNode.Data) is TFavoriteFolder then begin
304 FavFolder := TFavoriteFolder.Create;
305 CurrentNode := FTreeView.Items.AddChildObject(ParentNode, Node.Attributes['title'], FavFolder);
306 CurrentNode.ImageIndex := 14;
307 CurrentNode.SelectedIndex := 14;
308 CurrentNode.Expanded := Node.Attributes[ 'expanded' ] = 'true';
309 FStack.Push(CurrentNode);
311 for i := 0 to Node.ChildNodes.Count - 1 do begin
312 ReadNode(Node.ChildNodes[i]);
314 if FStack.Count <> 0 then
316 end else if Node.NodeName = 'favitem' then begin
318 ParentNode := FStack.Peek;
319 if TObject(ParentNode.Data) is TFavoriteFolder then begin
320 if Node.Attributes['favtype'] = 'board' then begin
322 //
\8b\8c\8e®
\82Ì
\82¨
\8bC
\82É
\93ü
\82è
\82Æ
\82Ì
\8cÝ
\8a·
\90«
\82Ì
\82½
\82ß
323 if Length( Node.Attributes[ 'bbs' ] ) > 0 then begin
324 board := BBSsFindBoardFromBBSID( Node.Attributes[ 'bbs' ] );
326 FavBoard := TFavoriteBoardItem.Create(
327 board.URL, Node.Attributes[ 'title' ], board );
329 FavBoard := TFavoriteBoardItem.Create(
330 Node.Attributes[ 'url' ], Node.Attributes[ 'title' ], nil );
332 CurrentNode := FTreeView.Items.AddChildObject(ParentNode, Node.Attributes['title'], FavBoard);
333 CurrentNode.ImageIndex := 15;
334 CurrentNode.SelectedIndex := 15;
335 end else if Node.Attributes['favtype'] = 'thread' then begin
336 //
\8b\8c\8e®
\82Ì
\82¨
\8bC
\82É
\93ü
\82è
\82Æ
\82Ì
\8cÝ
\8a·
\90«
\82Ì
\82½
\82ß
337 if Length( Node.Attributes[ 'bbs' ] ) > 0 then begin
338 board := BBSsFindBoardFromBBSID( Node.Attributes[ 'bbs' ] );
342 if not board.IsThreadDatRead then
343 GikoSys.ReadSubjectFile( board );
344 threadItem := board.Find( Node.Attributes[ 'thread' ] );
345 if threadItem = nil then begin
346 threadItem := TThreadItem.Create(
348 GikoSys.Get2chBoard2ThreadURL( board, ChangeFileExt( Node.Attributes[ 'thread' ], '' ) ) );
349 threadItem.Title := Node.Attributes[ 'title' ];
350 board.Add( threadItem );
352 FavThread := TFavoriteThreadItem.Create(
353 threadItem.URL, Node.Attributes[ 'title' ], threadItem );
356 FavThread := TFavoriteThreadItem.Create(
357 Node.Attributes[ 'url' ], Node.Attributes[ 'title' ], nil );
359 CurrentNode := FTreeView.Items.AddChildObject(ParentNode, Node.Attributes['title'], FavThread);
360 CurrentNode.ImageIndex := 16;
361 CurrentNode.SelectedIndex := 16;
365 //
\82±
\82Ì
\83A
\83C
\83e
\83\80\82Å
\96â
\91è
\82ª
\8bN
\82«
\82Ä
\82à
\91¼
\82Ì
\83A
\83C
\83e
\83\80\82É
\89e
\8b¿
\82ð
\97^
\82¦
\82½
\82
\82È
\82¢
\82Ì
\82Å
370 procedure TFavoriteDM.WriteFavorite;
374 // SaveList: TStringList;
378 XMLDoc: IXMLDocument;
380 // FavoNode: IXMLNode;
382 XMLDoc := NewXMLDocument;
383 XMLDoc.Encoding := 'Shift_JIS';
384 XMLDoc.StandAlone := 'yes';
385 XMLNode := XMLDoc.AddChild('favorite');
386 FileName := GikoSys.GetConfigDir + FAVORITE_FILE_NAME;
387 AddSaveString(TreeView.Items.GetFirstNode, XMLNode);
388 XMLDoc.SaveToFile(FileName);
392 SaveList: TStringList;
394 FileName := GikoSys.GetConfigDir + FAVORITE_FILE_NAME;
395 SaveList := TStringList.Create;
397 SaveList.Add('<?xml version="1.0" encoding="Shift_JIS" standalone="yes"?>');
398 SaveList.Add('<favorite>');
399 AddSaveString(TreeView.Items.GetFirstNode, SaveList);
400 SaveList.Add('</favorite>');
401 SaveList.SaveToFile(FileName);
408 procedure TFavoriteDM.AddSaveString(Node: TTreeNode; XMLNode: IXMLNode);
412 FavBoard: TFavoriteBoardItem;
413 FavThread: TFavoriteThreadItem;
416 for i := 0 to Node.Count - 1 do begin
417 if TObject(Node.Item[i].Data) is TFavoriteFolder then begin
418 FavNode := XMLNode.AddChild('folder');
419 FavNode.Attributes['title'] := Node.Item[i].Text;
420 AddSaveString(Node.Item[i], FavNode);
421 end else if TObject(Node.Item[i].Data) is TFavoriteBoardItem then begin
422 FavBoard := TFavoriteBoardItem(Node.Item[i].Data);
423 FavNode := XMLNode.AddChild('favitem');
424 FavNode.Attributes['type'] := '2ch';
425 FavNode.Attributes['favtype'] := 'board';
426 FavNode.Attributes['bbs'] := FavBoard.BBSID;
427 FavNode.Attributes['title'] := Node.Item[i].Text;
428 FavNode.Attributes['boardname'] := FavBoard.BoardName;
429 end else if TObject(Node.Item[i].Data) is TFavoriteThreadItem then begin
430 FavThread := TFavoriteThreadItem(Node.Item[i].Data);
431 FavNode := XMLNode.AddChild('favitem');
432 FavNode.Attributes['type'] := '2ch';
433 FavNode.Attributes['favtype'] := 'thread';
434 FavNode.Attributes['bbs'] := FavThread.BBSID;
435 FavNode.Attributes['thread'] := FavThread.ThreadID;
436 FavNode.Attributes['title'] := Node.Item[i].Text;
437 FavNode.Attributes['threadname'] := FavThread.ThreadName;
443 procedure TFavoriteDM.AddSaveString(Node: TTreeNode; SaveList: TStringList);
447 FavBoard: TFavoriteBoardItem;
448 FavThread: TFavoriteThreadItem;
450 for i := 0 to Node.Count - 1 do begin
451 if TObject(Node.Item[i].Data) is TFavoriteFolder then begin
452 if Node.Item[ i ].Expanded then
453 s := Format('<folder title="%s" expanded="true">', [HtmlEncode(Node.Item[i].Text)])
455 s := Format('<folder title="%s" expanded="false">', [HtmlEncode(Node.Item[i].Text)]);
457 AddSaveString(Node.Item[i], SaveList);
458 SaveList.Add('</folder>');
459 end else if TObject(Node.Item[i].Data) is TFavoriteBoardItem then begin
460 FavBoard := TFavoriteBoardItem(Node.Item[i].Data);
461 s := Format('<favitem type="2ch" favtype="board" url="%s" title="%s"/>',
462 [HtmlEncode( FavBoard.URL ), HtmlEncode(Node.Item[ i ].Text)]);
464 end else if TObject(Node.Item[i].Data) is TFavoriteThreadItem then begin
465 FavThread := TFavoriteThreadItem(Node.Item[i].Data);
466 s := Format('<favitem type="2ch" favtype="thread" url="%s" title="%s"/>',
467 [HtmlEncode( FavThread.URL ), HtmlEncode(Node.Item[ i ].Text)]);
474 procedure TFavoriteDM.SAXStartDocument(Sender: TObject);
476 FStack := TStack.Create;
477 FStack.Push(FTreeView.Items.GetFirstNode);
480 procedure TFavoriteDM.SAXEndDocument(Sender: TObject);
485 procedure TFavoriteDM.SAXStartElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString;
486 const Atts: IAttributes);
489 ParentNode: TTreeNode;
490 CurrentNode: TTreeNode;
491 FavFolder: TFavoriteFolder;
492 FavBoard: TFavoriteBoardItem;
493 FavThread: TFavoriteThreadItem;
495 if QName = 'folder' then begin
496 ParentNode := FStack.Peek;
497 if TObject(ParentNode.Data) is TFavoriteFolder then begin
498 FavFolder := TFavoriteFolder.Create;
499 CurrentNode := FTreeView.Items.AddChildObject(ParentNode, Atts.getValue('title'), FavFolder);
500 CurrentNode.ImageIndex := 12;
501 CurrentNode.SelectedIndex := 13;
502 FStack.Push(CurrentNode);
504 end else if QName = 'favitem' then begin
505 ParentNode := FStack.Peek;
506 if TObject(ParentNode.Data) is TFavoriteFolder then begin
507 FavType := Atts.getValue('favtype');
508 if FavType = 'board' then begin
509 FavBoard := TFavoriteBoardItem.Create;
510 FavBoard.BBSID := Atts.getValue('bbs');
511 FavBoard.BoardName := Atts.getValue('boardname');
512 CurrentNode := FTreeView.Items.AddChildObject(ParentNode, Atts.getValue('title'), FavBoard);
513 CurrentNode.ImageIndex := 14;
514 CurrentNode.SelectedIndex := 15;
515 end else if FavType = 'thread' then begin
516 FavThread := TFavoriteThreadItem.Create;
517 FavThread.BBSID := Atts.getValue('bbs');
518 FavThread.ThreadID := Atts.getValue('thread');
519 FavThread.ThreadName := Atts.getValue('threadname');
520 CurrentNode := FTreeView.Items.AddChildObject(ParentNode, Atts.getValue('title'), FavThread);
521 CurrentNode.ImageIndex := 16;
522 CurrentNode.SelectedIndex := 17;
528 procedure TFavoriteDM.SAXEndElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString);
530 if QName = 'folder' then begin
531 if FStack.Count <> 0 then
536 procedure TFavoriteDM.SAXCharacters(Sender: TObject; const PCh: SAXString);
541 function TFavoriteDM.SaveFavoriteFile(FileName: String) : Boolean;
543 FavoriteFilePath: string;
544 tempStringList: TStringList;
547 FavoriteFilePath := GikoSys.GetConfigDir + FAVORITE_FILE_NAME;
549 if FileExists( FavoriteFilePath ) then begin
550 tempStringList := TStringList.Create;
552 tempStringList.LoadFromFile( FavoriteFilePath );
553 tempStringList.SaveToFile( FileName );
563 procedure TFavoriteDM.URLReplace(oldURLs: TStringList; newURLs: TStringList);
565 FavoriteFilePath: string;
566 tempStringList: TStringList;
571 oldBoardName: string;
573 newBoardName: string;
577 FavoriteFilePath := GikoSys.GetConfigDir + FAVORITE_FILE_NAME;
579 if FileExists( FavoriteFilePath ) then begin
580 tempStringList := TStringList.Create;
582 tempStringList.LoadFromFile( FavoriteFilePath );
583 if oldURLs.Count = newURLs.Count then begin
584 //
\82±
\82ê
\82Å
\81ABoard
\82Í
\91S
\82Ä
\90Ø
\82è
\91Ö
\82í
\82é
585 for i := 0 to oldURLs.Count - 1 do begin
586 CustomStringReplace(tempStringList,oldURLs[i],newURLs[i],true);
588 //
\96Ê
\93|
\82¾
\82¯
\82Çthread
\82Í
\82»
\82ê
\82¼
\82êURL
\82ð
\83`
\83\83\83b
\83N
\82µ
\82È
\82ª
\82ç
\82â
\82Á
\82Ä
\82©
\82È
\82«
\82á
\82¢
\82¯
\82È
\82¢
\81B
589 for i := 0 to oldURLs.Count - 1 do begin
590 tmpURL := Copy(oldURLs[i], 1, Length(oldURLs[i]) -1);
591 oldHost := Copy(tmpURL, 1, LastDelimiter('/', tmpURL) );
592 oldBoardName := Copy(tmpURL, LastDelimiter('/', tmpURL), Length(tmpURL) ) + '/';
593 tmpURL := Copy(newURLs[i], 1, Length(newURLs[i]) -1);
594 newHost := Copy(tmpURL, 1, LastDelimiter('/', tmpURL) );
595 newBoardName := Copy(tmpURL, LastDelimiter('/', tmpURL), Length(tmpURL) ) + '/';
597 for j := 0 to tempStringList.Count - 1 do begin
598 tempString := tempStringList[j];
599 if AnsiPos('favtype="thread"',tempString ) <> 0 then begin
600 tempString := Copy(tempString, AnsiPos('url="',tempString ) + 5, AnsiPos('" title="',tempString ) -AnsiPos('url="',tempString ) - 5);
601 if ( AnsiPos(oldBoardName, tempString) <> 0 ) and ( AnsiPos(oldHost, tempString ) <> 0 ) then begin
602 tempString := StringReplace(tempStringList[j], oldHost, newHost,[]);
603 //tempString := StringReplace(tempString, oldBoardName, newBoardName,[]);
604 tempStringList[j] := tempString;
613 tempStringList.SaveToFile( FavoriteFilePath );
620 function TFavoriteDM.GetFavoriteFilePath() : String;
622 Result := GikoSys.GetConfigDir + FAVORITE_FILE_NAME;