OSDN Git Service

Current log can be closed
[winbottle/winbottle.git] / bottleclient / LogForm.pas
index 92a882c..f6efe8c 100755 (executable)
@@ -95,21 +95,21 @@ type
     procedure UpdateScriptScript(const Script: String);
     procedure mnURLClick(Sender: TObject);
     procedure ExtractURLs(Script: String; Result: TStrings);
-    function GetCurrentBottleLog: TBottleLogList;
     function GetDefaultFileName(const Name: String; const Ext: String): String;
+    function BottleLogTitled(const LogName: String): TBottleLogList;
   protected
     procedure CreateParams(var Params: TCreateParams); override;
   public
     { Public \90é\8c¾ }
     function SelectedBottleLog: TBottleLogList;
-    property CurrentBottleLog: TBottleLogList read GetCurrentBottleLog;
     property BottleLogList: TObjectList read FBottleLogList;
-    procedure AddCurrentScriptLog(const Script, Channel, MID, Ghost: String);
-    procedure AddCurrentSystemLog(const MessageString: String);
+    procedure AddCurrentScriptLog(const LogName, Script, Channel, MID, Ghost: String);
+    procedure AddCurrentSystemLog(const LogName, MessageString: String);
     procedure VoteLog(const MID: String; const Vote: integer);
     procedure AgreeLog(const MID: String; const Agree: integer);
     procedure SetBottleStatusToPlaying(const MID: String);
     procedure SetBottleStatusToOpened(const MID: String);
+    procedure AllBottleOpened;
     procedure LogLoaded(Sender: TObject);
     procedure LogLoadFailure(Sender: TObject; const Message: String);
     procedure UpdateTab;
@@ -121,8 +121,6 @@ type
 var
   frmLog: TfrmLog;
 
-function CurrentBottleLog: TBottleLogList;
-
 const
   IconBottle    = 17;
   IconOpened    = 30;
@@ -140,21 +138,16 @@ uses MainForm, StrUtils;
 
 {$R *.DFM}
 
-function CurrentBottleLog: TBottleLogList;
-begin
-  Result := frmLog.CurrentBottleLog;
-end;
-
 { TfrmLog }
 
-procedure TfrmLog.AddCurrentScriptLog(const Script, Channel, MID, Ghost: String);
+procedure TfrmLog.AddCurrentScriptLog(const LogName, Script, Channel, MID, Ghost: String);
 var Sel: integer;
 begin
-  CurrentBottleLog.AddScriptLog(Script, Channel, MID, Ghost);
-  if SelectedBottleLog <> CurrentBottleLog then Exit;
+  BottleLogTitled(LogName).AddScriptLog(Script, Channel, MID, Ghost);
+  if SelectedBottleLog <> BottleLogTitled(LogName) then Exit;
   lvwLog.OnChange := nil; //\83C\83x\83\93\83g\94­\90¶(\82¢\82ë\82¢\82ë\8dÄ\95`\89æ\82ª\8bN\82«\82é)\82Ì\97}\90§
   if lvwLog.Selected <> nil then Sel := lvwLog.Selected.Index else Sel := -1;
-  lvwLog.Items.Count := CurrentBottleLog.Count;
+  lvwLog.Items.Count := SelectedBottleLog.Count;
   UpdateWindow;
   if Sel >= 0 then begin
     lvwLog.Selected := lvwLog.Items[Sel + 1];
@@ -165,14 +158,14 @@ begin
   lvwLog.OnChange := lvwLogChange;
 end;
 
-procedure TfrmLog.AddCurrentSystemLog(const MessageString: String);
+procedure TfrmLog.AddCurrentSystemLog(const LogName, MessageString: String);
 var Sel: integer;
 begin
-  CurrentBottleLog.AddSystemLog(MessageString);
-  if SelectedBottleLog <> CurrentBottleLog then Exit;
+  BottleLogTitled(LogName).AddSystemLog(MessageString);
+  if SelectedBottleLog <> BottleLogTitled(LogName) then Exit;
   lvwLog.OnChange := nil;
   if lvwLog.Selected <> nil then Sel := lvwLog.Selected.Index else Sel := -1;
-  lvwLog.Items.Count := CurrentBottleLog.Count;
+  lvwLog.Items.Count := SelectedBottleLog.Count;
   UpdateWindow;
   if Sel >= 0 then begin
     lvwLog.Selected := lvwLog.Items[Sel + 1];
@@ -187,25 +180,18 @@ end;
 
 procedure TfrmLog.tbtnClearClick(Sender: TObject);
 begin
-  if SelectedBottleLog = CurrentBottleLog then begin
-    CurrentBottleLog.Clear;
-    lvwLog.Items.Count := 0;
-    lvwLog.Invalidate;
-    lvwLogChange(Self, nil, ctState);
-  end else begin
-    FBottleLogList.Delete(tabBottleLog.TabIndex);
-    tabBottleLog.TabIndex := 0;
-    UpdateTab;
-    UpdateWindow;
-    lvwLogChange(Self, nil, ctState);
-  end;
+  if SelectedBottleLog = nil then Exit;
+  FBottleLogList.Delete(tabBottleLog.TabIndex);
+  tabBottleLog.TabIndex := 0;
+  UpdateTab;
+  UpdateWindow;
+  lvwLogChange(Self, nil, ctState);
 end;
 
 procedure TfrmLog.FormCreate(Sender: TObject);
 var i: integer;
 begin
   FBottleLogList := TObjectList.Create;
-  FBottleLogList.Add(TBottleLogList.Create('\83J\83\8c\83\93\83g')); // CurrentBottleLog
 
   SsParser.TagPattern.Assign(frmSender.SsParser.TagPattern);
   SsParser.MetaPattern.Assign(frmSender.SsParser.MetaPattern);
@@ -255,31 +241,39 @@ procedure TfrmLog.lvwLogChange(Sender: TObject; Item: TListItem;
 var Script: String;
     Log: TLogItem;
 begin
-  StatusBar.Panels[0].Text := IntToStr(SelectedBottleLog.Count) + '\8c\8f';
-  if Change = ctState then begin
-    Script := '';
-    if lvwLog.Selected <> nil then begin
-      Log := SelectedBottleLog.Bottles[lvwLog.Selected.Index];
-      if (Log.LogType = ltBottle) and not frmSender.Connecting then begin
-        Script := Log.Script;
-        frmSender.actVoteMessage.Enabled := true;
-        frmSender.actAgreeMessage.Enabled := true;
-        mnPopUpCopyScript.Enabled := true;
-        UpdateScript(Script);
+  if SelectedBottleLog <> nil then begin
+    StatusBar.Panels[0].Text := IntToStr(SelectedBottleLog.Count) + '\8c\8f';
+    if Change = ctState then begin
+      Script := '';
+      if lvwLog.Selected <> nil then begin
+        Log := SelectedBottleLog.Bottles[lvwLog.Selected.Index];
+        if (Log.LogType = ltBottle) and not frmSender.Connecting then begin
+          Script := Log.Script;
+          frmSender.actVoteMessage.Enabled := true;
+          frmSender.actAgreeMessage.Enabled := true;
+          mnPopUpCopyScript.Enabled := true;
+          UpdateScript(Script);
+        end else begin
+          frmSender.actVoteMessage.Enabled := false;
+          frmSender.actAgreeMessage.Enabled := false;
+          mnPopUpCopyScript.Enabled := false;
+          UpdateScript(''); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\82ð\83N\83\8a\83A
+        end;
       end else begin
         frmSender.actVoteMessage.Enabled := false;
         frmSender.actAgreeMessage.Enabled := false;
         mnPopUpCopyScript.Enabled := false;
-        UpdateScript(''); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\82ð\83N\83\8a\83A
+        UpdateScript(Script); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\83N\83\8a\83A
       end;
-    end else begin
-      frmSender.actVoteMessage.Enabled := false;
-      frmSender.actAgreeMessage.Enabled := false;
-      mnPopUpCopyScript.Enabled := false;
-      UpdateScript(Script); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\83N\83\8a\83A
     end;
+    tbtnSaveLog.Enabled := lvwLog.Items.Count > 0;
+  end else begin
+    StatusBar.Panels[0].Text := '\83\8d\83O\82ª\82 \82è\82Ü\82¹\82ñ';
+    frmSender.actVoteMessage.Enabled := false;
+    frmSender.actAgreeMessage.Enabled := false;
+    mnPopUpCopyScript.Enabled := false;
+    UpdateScript(''); // \83\8d\83O\83v\83\8c\83r\83\85\81[\95\94\83N\83\8a\83A
   end;
-  tbtnSaveLog.Enabled := lvwLog.Items.Count > 0;
 end;
 
 procedure TfrmLog.lvwLogDblClick(Sender: TObject);
@@ -474,27 +468,38 @@ begin
 end;
 
 procedure TfrmLog.SetBottleStatusToOpened(const MID: String);
+var i: integer;
+    Bottle: TLogItem;
 begin
-  if CurrentBottleLog.Bottle(MID) <> nil then begin
-    CurrentBottleLog.Bottle(MID).State := lsOpened;
-    lvwLog.OnChange := nil;
-    lvwLog.Invalidate;
-    lvwLog.OnChange := lvwLogChange;
+  for i := 0 to FBottleLogList.Count-1 do begin
+    Bottle := (FBottleLogList[i] as TBottleLogList).Bottle(MID);
+    if Bottle <> nil then begin
+      Bottle.State := lsOpened;
+      lvwLog.OnChange := nil;
+      lvwLog.Invalidate;
+      lvwLog.OnChange := lvwLogChange;
+    end;
   end;
 end;
 
 procedure TfrmLog.SetBottleStatusToPlaying(const MID: String);
+var i: integer;
+    Bottle: TLogItem;
 begin
-  if CurrentBottleLog.Bottle(MID) <> nil then begin
-    CurrentBottleLog.Bottle(MID).State := lsPlaying;
-    lvwLog.OnChange := nil;
-    lvwLog.Invalidate;
-    lvwLog.OnChange := lvwLogChange;
+  for i := 0 to FBottleLogList.Count-1 do begin
+    Bottle := (FBottleLogList[i] as TBottleLogList).Bottle(MID);
+    if Bottle <> nil then begin
+      Bottle.State := lsPlaying;
+      lvwLog.OnChange := nil;
+      lvwLog.Invalidate;
+      lvwLog.OnChange := lvwLogChange;
+    end;
   end;
 end;
 
 procedure TfrmLog.mnSaveLogClick(Sender: TObject);
 begin
+  if SelectedBottleLog = nil then Exit;
   SaveDialog.FileName := GetDefaultFileName(SelectedBottleLog.Title, '.log');
   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
   SaveDialog.DefaultExt := 'log';
@@ -505,6 +510,7 @@ end;
 
 procedure TfrmLog.mnSaveLogChannelClick(Sender: TObject);
 begin
+  if SelectedBottleLog = nil then Exit;
   SaveDialog.FileName := GetDefaultFileName(SelectedBottleLog.Title, '.log');
   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
   SaveDialog.DefaultExt := 'log';
@@ -515,6 +521,7 @@ end;
 
 procedure TfrmLog.mnSaveLogScriptClick(Sender: TObject);
 begin
+  if SelectedBottleLog = nil then Exit;
   SaveDialog.FileName := GetDefaultFileName(SelectedBottleLog.Title, '.txt');
   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
   SaveDialog.DefaultExt := 'txt';
@@ -525,6 +532,7 @@ end;
 
 procedure TfrmLog.mnSaveLogXMLClick(Sender: TObject);
 begin
+  if SelectedBottleLog = nil then Exit;
   SaveDialog.FileName := GetDefaultFileName(SelectedBottleLog.Title, '.xml');
   SaveDialog.InitialDir := ExtractFileDir(Application.ExeName);
   SaveDialog.DefaultExt := 'xml';
@@ -573,8 +581,8 @@ begin
 end;
 
 procedure TfrmLog.UpdateWindow;
+var EnabledFlag: boolean;
 begin
-  StatusBar.Panels[0].Text := IntToStr(SelectedBottleLog.Count) + '\8c\8f';
   if Pref.ColorScript then begin
     if lvwLog.Color <> Pref.BgColor then lvwLog.Color := Pref.BgColor;
     if lvwLog.Font.Color <> Pref.TalkColorH then lvwLog.Font.Color := Pref.TalkColorH;
@@ -582,9 +590,20 @@ begin
     if lvwLog.Color <> clWindow then lvwLog.Color := clWindow;
     if lvwLog.Font.Color <> clWindowText then lvwLog.Font.Color := clWindowText;
   end;
-  lvwLog.Items.Count := SelectedBottleLog.Count;
+  if SelectedBottleLog <> nil then begin
+    StatusBar.Panels[0].Text := IntToStr(SelectedBottleLog.Count) + '\8c\8f';
+    lvwLog.Items.Count := SelectedBottleLog.Count;
+  end else begin
+    StatusBar.Panels[0].Text := '\83\8d\83O\82ª\82 \82è\82Ü\82¹\82ñ';
+    lvwLog.Items.Count := 0;
+  end;
+
+  EnabledFlag := SelectedBottleLog <> nil;
+  tbtnClear.Enabled := EnabledFlag;
+  tbtnSaveLog.Enabled := EnabledFlag;
+  tbtnFindBottle.Enabled := EnabledFlag;
+
   lvwLog.Invalidate;
-  //lvwLogChange(Self, lvwLog.Selected, ctState);
 end;
 
 procedure TfrmLog.PopupMenuListViewPopup(Sender: TObject);
@@ -824,12 +843,10 @@ end;
 
 function TfrmLog.SelectedBottleLog: TBottleLogList;
 begin
-  Result := FBottleLogList.Items[tabBottleLog.TabIndex] as TBottleLogList;
-end;
-
-function TfrmLog.GetCurrentBottleLog: TBottleLogList;
-begin
-  Result := FBottleLogList.Items[0] as TBottleLogList;
+  if tabBottleLog.TabIndex >= 0 then
+    Result := FBottleLogList.Items[tabBottleLog.TabIndex] as TBottleLogList
+  else
+    Result := nil;
 end;
 
 procedure TfrmLog.tabBottleLogChange(Sender: TObject);
@@ -850,15 +867,19 @@ begin
 end;
 
 procedure TfrmLog.UpdateTab;
-var i: integer;
-    cur: TBottleLogList;
+var i, cur: integer;
 begin
-  cur := SelectedBottleLog;
+  cur := tabBottleLog.tabIndex;
   tabBottleLog.Tabs.Clear;
   for i := 0 to FBottleLogList.Count - 1 do begin
     tabBottleLog.Tabs.Add((FBottleLogList[i] as TBottleLogList).Title);
   end;
-  tabBottleLog.TabIndex := FBottleLogList.IndexOf(cur);
+  if FBottleLogList.Count > 0 then begin
+    if cur < FBottleLogList.Count then
+      tabBottleLog.TabIndex := cur
+    else
+      tabBottleLog.TabIndex := FBottleLogList.Count-1;
+  end;
 end;
 
 procedure TfrmLog.LogLoadFailure(Sender: TObject; const Message: String);
@@ -900,6 +921,8 @@ end;
 procedure TfrmLog.tabBottleLogChanging(Sender: TObject;
   var AllowChange: Boolean);
 begin
+  // \8c»\8dÝ\91I\91ð\82³\82ê\82Ä\82¢\82é\83\8d\83O\82Ì\91I\91ð\8fó\91Ô\82ð\95Û\91
+  if SelectedBottleLog = nil then Exit;
   if lvwLog.Selected <> nil then
     SelectedBottleLog.SelectedIndex := lvwLog.Selected.Index
   else
@@ -912,15 +935,12 @@ begin
   with tabBottleLog do begin
     Tag := IndexOfTabAt(MousePos.X, MousePos.Y);
     if Tag < 0 then Handled := true;
-    mnCloseTab.Enabled := Tag > 0;
   end;
 end;
 
 procedure TfrmLog.mnCloseTabClick(Sender: TObject);
 begin
-  if tabBottleLog.Tag = 0 then Exit;
   FBottleLogList.Delete(tabBottleLog.Tag);
-  tabBottleLog.TabIndex := 0;
   UpdateTab;
   UpdateWindow;
   lvwLogChange(Self, nil, ctState);
@@ -932,6 +952,7 @@ var Query: String;
     Item1, Item2: TLogItem;
     i, matched: integer;
 begin
+  if SelectedBottleLog = nil then Exit;
   if SelectedBottleLog.Count = 0 then begin
     ShowMessage('\8c\9f\8dõ\91Î\8fÛ\82ª\8bó\82Å\82·\81B');
     Exit;
@@ -964,24 +985,25 @@ end;
 
 procedure TfrmLog.tbtnOpenLogClick(Sender: TObject);
 var BottleLog: TBottleLogList;
-    i: integer;
+    i, Index: integer;
 begin
-  BottleLog := nil;
+  Index := -1;
   if OpenDialog.Execute then begin
-    try
-      for i := 0 to OpenDialog.Files.Count-1 do begin
-        BottleLog := TBottleLogList.Create(ExtractFileName(OpenDialog.Files[i]));
+    for i := 0 to OpenDialog.Files.Count-1 do begin
+      BottleLog := TBottleLogList.Create(ExtractFileName(OpenDialog.Files[i]));
+      try
         BottleLog.LoadFromXMLFile(OpenDialog.Files[i], XMLDocument);
-        BottleLogList.Add(BottleLog);
-      end;
-    except
-      on E: EXMLFileOpenException do begin
-        Beep;
-        ShowMessage(E.Message);
-        BottleLog.Free;
+      except
+        on E: EXMLFileOpenException do begin
+          Beep;
+          ShowMessage(E.Message);
+          FreeAndNil(BottleLog);
+        end;
       end;
+      if BottleLog <> nil then Index := BottleLogList.Add(BottleLog); // \8dÅ\8cã\82É\8aJ\82¢\82½\83\8d\83O\82Ì\88Ê\92u\82ð\8bL\89¯
     end;
     UpdateTab;
+    if Index >= 0 then tabBottleLog.TabIndex := Index;
     UpdateWindow;
   end;
 end;
@@ -993,4 +1015,31 @@ begin
   Result := ChangeFileExt(Result, Ext);
 end;
 
+function TfrmLog.BottleLogTitled(const LogName: String): TBottleLogList;
+var i: integer;
+begin
+  for i := 0 to FBottleLogList.Count-1 do begin
+    if (FBottleLogList[i] as TBottleLogList).Title = LogName then begin
+      Result := (FBottleLogList[i] as TBottleLogList);
+      Exit;
+    end;
+  end;
+  // \8c©\82Â\82©\82ç\82È\82¢\8fê\8d\87
+  Result := TBottleLogList.Create(LogName); // \90V\82µ\82­\8dì\82é
+  FBottleLogList.Add(Result);
+  UpdateTab;
+end;
+
+procedure TfrmLog.AllBottleOpened;
+var i, j: integer;
+    Log: TBottleLogList;
+begin
+  for i := 0 to FBottleLogList.Count-1 do begin
+    Log  := FBottleLogList[i] as TBottleLogList;
+    for j := 0 to Log.Count-1 do begin
+      Log.Bottles[j].State := lsOpened;
+    end;
+  end;
+end;
+
 end.