OSDN Git Service

初回Tabファイル保存時にエラーメッセージが出るのを修正
[gikonavigoeson/gikonavi.git] / ItemDownload.pas
index 932c22b..fe0fb18 100644 (file)
@@ -5,7 +5,8 @@ interface
 uses
        Windows, SysUtils, Classes, ComCtrls, Controls, Forms, IdHTTP,
        {HTTPApp,} YofUtils, IdGlobal, IdException, IdComponent, IniFiles, {DateUtils,}
-       GikoSystem, BoardGroup, MonaUtils, ExternalBoardManager, ExternalBoardPlugInMain;
+       GikoSystem, BoardGroup, MonaUtils, ExternalBoardManager, ExternalBoardPlugInMain,
+       Sort;
 
 type
        TDownloadItem = class;
@@ -59,7 +60,7 @@ type
                property Item: TDownloadItem read FItem write FItem;
                property Number: Integer read FNumber write FNumber;
                constructor Create(CreateSuspended: Boolean);
-        destructor Destroy; override;
+                               destructor Destroy; override;
                procedure Abort;
                property OnWork: TGikoWorkEvent read FOnWork write FOnWork;
                property OnWorkBegin: TGikoWorkBeginEvent read FOnWorkBegin write FOnWorkBegin;
@@ -81,6 +82,7 @@ type
                FState: TGikoDownloadState;
                FErrText: string;
                FForceDownload: Boolean;
+               FIsAbone : Boolean;
        public
                procedure SaveListFile;
                procedure SaveItemFile;
@@ -96,6 +98,7 @@ type
                property State: TGikoDownloadState read FState write FState;
                property ErrText: string read FErrText write FErrText;
                property ForceDownload: Boolean read FForceDownload write FForceDownload;
+               property IsAbone : Boolean read FIsAbone write FIsAbone;
        end;
 
 implementation
@@ -153,10 +156,12 @@ var
        Idx: Integer;
        ATitle: string;
        DownloadResult: Boolean;
-       Abone: Boolean;
-       foundPos: Integer;
-       boardPlugIn : TBoardPlugIn;
-       listContent : string;
+       boardPlugIn     : TBoardPlugIn;
+       lastContent             : string;
+       logFile                         : TFileStream;
+       adjustMargin    : Integer;
+const
+       ADJUST_MARGIN   = 16;
 begin
        while not Terminated do begin
                //===== \83v\83\89\83O\83C\83\93
@@ -249,6 +254,25 @@ begin
                        {$ENDIF}
                end;
 
+               adjustMargin := 0;
+               if Item.DownType = gdtThread then begin
+                       if FileExists( Item.ThreadItem.GetThreadFileName ) then begin
+                               // dat \83t\83@\83C\83\8b\82Ì\8dÅ\8cã\82ð\93Ç\82Ý\8fo\82·
+                               SetLength( lastContent, ADJUST_MARGIN + 1 );
+                               logFile := TFileStream.Create( Item.ThreadItem.GetThreadFileName, fmOpenRead or fmShareDenyWrite );
+                               try
+                                       logFile.Seek( -(ADJUST_MARGIN + 1), soFromEnd );
+                                       logFile.Read( lastContent[ 1 ], ADJUST_MARGIN + 1 );
+                                       lastContent := StringReplace( lastContent, #13, '', [] );       // CR \82Ì\8dí\8f\9c
+                               finally
+                                       logFile.Free;
+                               end;
+                       end else begin
+        lastContent := '';
+                       end;
+                       adjustMargin := Length( lastContent );
+               end;
+
                FIndy.Request.ContentRangeStart := 0;
                FIndy.Request.LastModified := ZERO_DATE;
                ResStream := TMemoryStream.Create;
@@ -308,23 +332,25 @@ begin
                                                        {$IFDEF DEBUG}
                                                        Writeln('RangeStart: ' + IntToStr(Item.ThreadItem.Size));
                                                        {$ENDIF}
-                                                       //\82 \82Ú\81[\82ñ\83`\83F\83b\83N\82Ì\82½\82ß\82P\83o\83C\83g\91O\82©\82ç\8eæ\93¾
+                                                       // \82 \82Ú\81[\82ñ\83`\83F\83b\83N\82Ì\82½\82ß adjustMargin \83o\83C\83g\91O\82©\82ç\8eæ\93¾
                                                        RangeStart := Item.ThreadItem.Size;
-                                                       AdjustLen := -1;
+                                                       AdjustLen := -adjustMargin;
                                                end;
                                        end;
                                end;
-                               Abone := False;
+                               Item.IsAbone := False;
                                DownloadResult := DatDownload(Item.DownType, URL, Modified, RangeStart, AdjustLen);
                                {$IFDEF DEBUG}
                                Writeln('ResponseCode: ' + IntToStr(FIndy.ResponseCode));
                                {$ENDIF}
                                if Item.DownType = gdtThread then begin
                                        if Item.ResponseCode = 416 then begin
-                                               Abone := True;
+                                               Item.IsAbone := True;
                                                DownloadResult := True;
-                                       end else if DownloadResult and (AdjustLen = -1) and (Item.Content[1] <> #10) then
-                                               Abone := True;
+                                       end else if DownloadResult and (AdjustLen < 0) then begin
+                                               if Copy( Item.Content, 1, adjustMargin ) <> lastContent then
+                                                       Item.IsAbone := True;
+                                       end;
                                end;
 
                                if Trim(FIndy.Response.RawHeaders.Values['Date']) <> '' then begin
@@ -338,7 +364,7 @@ begin
                                        {$IFDEF DEBUG}
                                        Writeln('Date:' + FIndy.Response.RawHeaders.Values['Date']);
                                        {$ENDIF}
-                                       if Abone then begin
+                                       if Item.IsAbone then begin
                                                {$IFDEF DEBUG}
                                                Writeln('\82 \82Ú\81[\82ñ\8c\9f\8fo');
                                                {$ENDIF}
@@ -360,9 +386,9 @@ begin
                                                Writeln('\82 \82Ú\81[\82ñ\8dÄ\8eæ\93¾\8cã');
                                                Writeln('ResponseCode: ' + IntToStr(Item.ResponseCode));
                                                {$ENDIF}
-                                       end else if (Item.DownType = gdtThread) and (AdjustLen = -1) and (Item.Content[1] = #10) then begin
-                                               //\8d·\95ª\8eæ\93¾\82©\82Â\82P\83o\83C\83g\96Ú\82ªLF\82Ì\8fê\8d\87\81i\90³\8fí\8eæ\93¾\81j\82Í\93ª\82ÌLF\82ð\8dí\8f\9c
-                                               Item.Content := Copy(Item.Content, 2, Length(Item.Content));
+                                       end else if (Item.DownType = gdtThread) and (AdjustLen 0) then begin
+                                               // \8d·\95ª\8eæ\93¾\82ª\8fo\97\88\82½\8fê\8d\87\82Í\82 \82Ú\81[\82ñ\83`\83F\83b\83N\97p\82É\8eæ\93¾\82µ\82½\97]\95ª\82È\83T\83C\83Y\82ð\8dí\8f\9c
+                                               Item.Content := Copy(Item.Content, adjustMargin + 1, MaxInt);
                                        end;
                                end else begin
                                        Item.State := gdsError;
@@ -963,6 +989,16 @@ begin
                Board.RoundDate := Now;
                //\83T\81[\83o\8fã\83t\83@\83C\83\8b\82Ì\8dX\90V\8e\9e\8d\8f\90Ý\92è
                Board.LastModified := LastModified;
+
+               //dat\97\8e\82¿\83X\83\8c\82Ì\83\\81[\83g\8f\87\82ð\8c\88\92è\82·\82é\82½\82ß\82É\83\\81[\83g\82·\82é
+               if GikoSys.Setting.DatOchiSortIndex >= 0 then begin
+                       Sort.SortNoFlag := true;
+                       Sort.SortOrder := GikoSys.Setting.DatOchiSortOrder;
+                       Sort.SortIndex := GikoSys.Setting.DatOchiSortIndex;
+                       //Sort.SortNonAcquiredCountFlag := GikoSys.Setting.NonAcquiredCount;
+                       Board.CustomSort(ThreadItemSortProc);
+               end;
+
                for i := Board.Count - 1 downto 0 do
                        Board.Items[i].AgeSage := gasNull;
 
@@ -1139,54 +1175,105 @@ end;
 }
 procedure TDownloadItem.SaveItemFile;
 var
-       Body: TStringList;
+       Body, oldBody: TStringList;
        Cnt: Integer;
        OldCnt: Integer;
        FileName: string;
        ini: TMemIniFile;
        Res: TResRec;
        NewRes: Integer;
-    finish : Boolean;
-    loopCnt : Integer;
+       finish : Boolean;
+       loopCnt : Integer;
+//     KokoTxt : string;
+//     KokoIdx : Integer;
+//     NewTxt  : string;
+//     NewIdx  : Integer;
+//     LastTxt : string;
+       LastIdx : Integer;
 begin
        FileName := ThreadItem.GetThreadFileName;
 
        if not ThreadItem.IsBoardPlugInAvailable then begin
                if Trim(Content) = '' then
                        Exit;
+
                GikoSys.ForceDirectoriesEx(ExtractFilePath(FileName));
 
-       //      Cnt := 0;
+               //      Cnt := 0;
                Body := TStringList.Create;
-        NewRes := 0;
-        OldCnt := 0;
+               NewRes := 0;
+               OldCnt := 0;
                try
-       //              if FileExists(FileName) and (ResponseCode = 206) then begin
+               //              if FileExists(FileName) and (ResponseCode = 206) then begin
                        if FileExists(FileName) and (State = gdsDiffComplete) then begin
-       //                      Body.Text := Content;
-       //                      if Body.Count > 0 then
-       //                              Body.Delete(0);
-       //                      Content := Body.Text;
-                loopCnt := 10;
+                               loopCnt := 10;
                                repeat
-                    finish := true;
-                       try
+                                       finish := true;
+                                       try
                                                Body.LoadFromFile(FileName);
                                                OldCnt := Body.Count;
                                                Body.Text := Body.Text + Content;
                                                Body.SaveToFile(FileName);
                                                NewRes := Body.Count - OldCnt;
-                       except
-                               on E:EFOpenError do begin
-                            sleep(10);
-                               finish := false;
-                            Dec(loopCnt);
-                        end;
-                       end;
-                until finish and ( loopCnt > 0 );
+                                       except
+                                               on E:EFOpenError do begin
+                                                       sleep(10);
+                                                       Dec(loopCnt);
+                                                       if loopCnt > 0 then
+                                                               finish := false;
+                                               end;
+                                       end;
+                               until finish;
                                //Cnt := Body.Count;
                        end else begin
-                               Body.Text := Content;
+                               if IsAbone then begin
+                                       // \82 \82Ú\81[\82ñ\82ð\8c\9f\8fo\82µ\82½\82Ì\82Å\82±\82±\82Ü\82Å\93Ç\82ñ\82¾\82Æ\90V\92\85\83\8c\83X\94Ô\82Ì\82Â\82¯\82È\82¨\82µ
+                                       oldBody := TStringList.Create;
+                                       try
+                                               loopCnt := 10;
+                                               repeat
+                                                       finish := true;
+                                                       try
+                                                               oldBody.LoadFromFile(FileName);
+                                                       except
+                                                               on E:EFOpenError do begin
+                                                                       sleep(10);
+                                                                       Dec(loopCnt);
+                                                                       if loopCnt > 0 then
+                                                                               finish := false
+                                                                       else
+                                                                               finish := true;
+                                                               end;
+                                                       end;
+                                               until finish;
+
+                                               Body.Text := Content;
+                                               if (ThreadItem.Kokomade > 0) and (ThreadItem.Kokomade <= oldBody.Count) then begin
+                                                       ThreadItem.Kokomade := Body.IndexOf(oldBody.Strings[ ThreadItem.Kokomade - 1 ]);
+                                                       if ThreadItem.Kokomade <> -1 then ThreadItem.Kokomade := ThreadItem.Kokomade + 1;
+                                               end;
+
+                                               LastIdx := oldBody.Count;
+                                               repeat
+                                                       Dec(LastIdx);
+                                                       OldCnt := Body.IndexOf(oldBody.Strings[ LastIdx ]) + 1;
+                                               until ( OldCnt <> 0 ) or (LastIdx = 0);
+
+                                               if OldCnt >= Body.Count then OldCnt := Body.Count - 1;
+                                               NewRes := Body.Count - OldCnt;
+
+
+                                       finally
+                                               oldBody.Free;
+                                       end;
+
+                               end else begin
+                                       Body.Text := Content;
+                                       //ThreadItem.Count := 0;
+                                       OldCnt := 0;
+                                       NewRes := Body.Count;
+                                       //Cnt := Body.Count;
+                               end;
        //                      if Body.Count > 0 then
        //                              Body.Delete(0);
                                Body.SaveToFile(FileName);
@@ -1196,18 +1283,12 @@ begin
                                        ThreadItem.Title := Res.FTitle;
                                end;
                                ThreadItem.Size := 0;
-                               //ThreadItem.Count := 0;
-                               ThreadItem.AllResCount := 0;
-                               ThreadItem.NewResCount := 0;
-                               OldCnt := 0;
-                               NewRes := Body.Count;
-                               //Cnt := Body.Count;
                        end;
                        Cnt := Body.Count;
                finally
                        Body.Free;
                end;
-        
+
                ThreadItem.Size := ThreadItem.Size + ContentLength;
                ThreadItem.LastModified := LastModified;
                ThreadItem.Count := Cnt;
@@ -1215,11 +1296,13 @@ begin
                ThreadItem.NewResCount := NewRes;
                ThreadItem.NewReceive := OldCnt + 1;
        end;
-    ThreadItem.AllResCount := ThreadItem.Count;
+       ThreadItem.AllResCount := ThreadItem.Count;
        ThreadItem.IsLogFile := True;
        ThreadItem.RoundDate := Now;
-       ThreadItem.UnRead := True;
-       ThreadItem.ParentBoard.UnRead := ThreadItem.ParentBoard.UnRead + 1;
+       if not ThreadItem.UnRead then begin
+               ThreadItem.UnRead := True;
+               ThreadItem.ParentBoard.UnRead := ThreadItem.ParentBoard.UnRead + 1;
+       end;
 //     if ThreadItem.RoundNo = 6 then
 //             ThreadItem.RoundNo := 0;
 
@@ -1238,7 +1321,7 @@ begin
 //             ini.WriteInteger('Setting', 'RoundNo', ThreadItem.RoundNo);
                ini.WriteBool('Setting', 'Round', ThreadItem.Round);
                ini.WriteBool('Setting', 'UnRead', ThreadItem.UnRead);
-        ini.UpdateFile;
+               ini.UpdateFile;
        finally
                ini.Free;
        end;