Skip to content

Commit

Permalink
Merge pull request #19 from perappu/feature/exportlogs
Browse files Browse the repository at this point in the history
Feature/exportlogs
  • Loading branch information
perappu authored Jul 26, 2023
2 parents b061c65 + 2199334 commit 8c71ae3
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 20 deletions.
2 changes: 1 addition & 1 deletion WhoSaidWhatNow/Objects/ChatEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public string CreateMessage(string tag)
{
string time = this.Time.ToShortTimeString();
string sender = this.Sender.Name + "" + this.Sender.Server;
string msg = this.Message;
string msg = this.Message.Trim();

return $"[{time}]" + String.Format(tag, sender, msg);
}
Expand Down
7 changes: 7 additions & 0 deletions WhoSaidWhatNow/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
using Dalamud.Interface.ImGuiFileDialog;
using Dalamud.Interface.Windowing;
using Dalamud.IoC;
using Dalamud.Plugin;
Expand Down Expand Up @@ -61,6 +62,8 @@ public sealed class Plugin : IDalamudPlugin
[RequiredVersion("1.0")]
public static ObjectTable ObjectTable { get; private set; } = null!;

public FileDialogManager FileDialogManager { get; set; } = null!;

internal ChatListener ChatListener { get; private set; } = null!;

public PlayerService PlayerService { get; set; } = null!;
Expand All @@ -77,13 +80,15 @@ public Plugin()
// setup UI
this.MainWindow = new MainWindow(this);
this.ConfigWindow = new ConfigWindow(this);
this.FileDialogManager = new FileDialogManager();

this.WindowSystem = new WindowSystem("WhoSaidWhatNow");
this.WindowSystem.AddWindow(this.ConfigWindow);
this.WindowSystem.AddWindow(this.MainWindow);

PluginInterface.UiBuilder.Draw += DrawUI;
PluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
PluginInterface.UiBuilder.Draw += FileDialogManager.Draw;

// add events/listeners
Plugin.ClientState.Login += OnLogin;
Expand All @@ -96,6 +101,7 @@ public Plugin()
{
HelpMessage = "Open main window"
});

}

//TODO: make sure we're disposing of everything we need to appropriately
Expand All @@ -104,6 +110,7 @@ public void Dispose()
ChatListener.Dispose();
PluginInterface.UiBuilder.Draw -= DrawUI;
PluginInterface.UiBuilder.OpenConfigUi -= DrawConfigUI;
PluginInterface.UiBuilder.Draw -= FileDialogManager.Draw;
WindowSystem.RemoveAllWindows();
CommandManager.RemoveHandler(COMMAND);
Plugin.ClientState.Login -= OnLogin;
Expand Down
111 changes: 111 additions & 0 deletions WhoSaidWhatNow/Services/FileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Dalamud.DrunkenToad;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using WhoSaidWhatNow.Objects;

namespace WhoSaidWhatNow.Services
{
public class FileService
{
/// <summary>
/// OpenFileDialog for individual
/// </summary>
/// <param name="plugin">the plugin</param>
/// <param name="playerName">player name</param>
public static void OpenFileDialog(Plugin plugin, string playerName)
{
plugin.FileDialogManager.SaveFileDialog("Save log...", "Text File{.txt}",
Regex.Replace(playerName, "[^a-zA-Z0-9]", String.Empty) + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",
".txt", (isOk, selectedFile) =>
{
if (isOk)
{
FileService.SaveIndividualLog(selectedFile);
}
});
}

/// <summary>
/// OpenFileDialog for groups
/// </summary>
/// <param name="plugin">the plugin</param>
/// <param name="group">KeyValuePair string for group name, Dictionary<Player, Boolean> for contents of group</param>
public static void OpenFileDialog(Plugin plugin, KeyValuePair<string, (string NAME, Dictionary<Player, bool> PLAYERS)> group)
{
plugin.FileDialogManager.SaveFileDialog("Save log...", "Text File{.txt}",
Regex.Replace(group.Key, "[^a-zA-Z0-9]", String.Empty) + "-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",
".txt", (isOk, selectedFile) =>
{
if (isOk)
{
FileService.SaveGroupLog(selectedFile, group.Value.PLAYERS);
}
});
}

/// <summary>
/// Save individual character log to file
/// </summary>
/// <param name="path">file path passed from save file dialog</param>
public static void SaveIndividualLog(string path)
{
try
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, false))
{
foreach (var c in from KeyValuePair<DateTime, ChatEntry> c in Plugin.ChatEntries
where Plugin.Config.ChannelToggles[c.Value.Type] == true && c.Value.Sender.Name.Contains(Plugin.SelectedPlayer.Name)
select c)
{
string tag = Plugin.Config.Formats[c.Value.Type];
file.WriteLine(c.Value.CreateMessage(tag));
}
ChatGuiExtensions.PluginPrint(Plugin.ChatGui, "Successfully saved log: " + path);
}
}
catch
{
ChatGuiExtensions.PluginPrint(Plugin.ChatGui, "Failed to save log.");
}
}


/// <summary>
/// Save group log to file
/// </summary>
/// <param name="path">file path passed from save file dialog</param>
/// <param name="players">passed dictionary of players</param>
public static void SaveGroupLog(string path, Dictionary<Player, Boolean> players)
{
try
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, false))
{
foreach (var c in Plugin.ChatEntries)
{
// if we are displaying this type of message;
if (Plugin.Config.ChannelToggles[c.Value.Type] == true)
{
// and if the player is among the tracked;
var p = Plugin.Players.Find(p => c.Value.Sender.Name.Contains(p.Name));
if (players[p!])
{
string tag = Plugin.Config.Formats[c.Value.Type];
file.WriteLine(c.Value.CreateMessage(tag));
}
}
}

ChatGuiExtensions.PluginPrint(Plugin.ChatGui, "Successfully saved log: " + path);
}
}
catch
{
ChatGuiExtensions.PluginPrint(Plugin.ChatGui, "Failed to save log.");
}
}

}
}
4 changes: 2 additions & 2 deletions WhoSaidWhatNow/WhoSaidWhatNow.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"Punchline": "Per character chat history plugin.",
"Description": "Per character chat history plugin, evolution of Maia-Everett/dalamud-snooper.\n Main command /whowhat",
"InternalName": "WhoSaidWhatNow",
"AssemblyVersion": "0.1.1",
"TestingAssemblyVersion": "0.1.1",
"AssemblyVersion": "0.2.1",
"TestingAssemblyVersion": "0.2.1",
"ApplicableVersion": "any",
"RepoUrl": "https://github.com/perappu/WhoSaidWhatNow",
"DalamudApiLevel": 8,
Expand Down
6 changes: 3 additions & 3 deletions WhoSaidWhatNow/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void AddPlayerSelectable(Player player)
{
ImGui.BeginGroup();

if (ImGui.Selectable("###WhoSaidWhatNow_Player_Selectable_" + player.Name, true, ImGuiSelectableFlags.None))
if (ImGui.Selectable("###WhoSaidWhatNow_Player_Selectable_" + player.Name, player.Name.Equals(Plugin.SelectedPlayer?.Name), ImGuiSelectableFlags.None))
{
ToggleWindowOpen(player);
}
Expand Down Expand Up @@ -185,10 +185,10 @@ public override void Draw()

//INDIVIDUAL TAB
ImGui.BeginTabBar("###WhoSaidWhatNow_Tab_Bar");
var individual = new TabIndividual(this);
var individual = new TabIndividual(this, plugin);

//GROUP TAB
var groups = new TabGroups(this);
var groups = new TabGroups(this, plugin);

ImGui.EndTabBar();

Expand Down
26 changes: 22 additions & 4 deletions WhoSaidWhatNow/Windows/TabGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
using WhoSaidWhatNow;
using WhoSaidWhatNow.Windows;
using System.Linq;

using System.Text.RegularExpressions;
using WhoSaidWhatNow.Services;

public class TabGroups
{
private static int counter = 1;

public TabGroups(MainWindow main)
public TabGroups(MainWindow main, Plugin plugin)
{


if (ImGui.BeginTabItem("Groups"))
{
main.toggleWindow(true);
Expand Down Expand Up @@ -55,7 +55,25 @@ public TabGroups(MainWindow main)
ImGui.SameLine();

// construct chatlog.
ImGui.BeginChild(MainWindow.ID_PANEL_RIGHT, new Vector2(0, 0), true);
ImGui.BeginChild(MainWindow.ID_PANEL_RIGHT, new Vector2(0, 0), true, ImGuiWindowFlags.MenuBar);

// add menu bar with chat log button
if (ImGui.BeginMenuBar())
{
//push font to make our menus with FA icons
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.MenuItem(FontAwesomeIcon.Save.ToIconString()))
{
FileService.OpenFileDialog(plugin, g);
}
ImGui.PopFont();
if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Save log to .txt file");
}
ImGui.EndMenuBar();
}

ImGui.EndChild();

ImGui.BeginChild(MainWindow.ID_PANEL_RIGHT);
Expand Down
55 changes: 45 additions & 10 deletions WhoSaidWhatNow/Windows/TabIndividual.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using Dalamud.Game.ClientState.Objects.Enums;
using Dalamud.Interface;
using Dalamud.Logging;
using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using WhoSaidWhatNow.Objects;
using WhoSaidWhatNow.Services;

namespace WhoSaidWhatNow.Windows;

public class TabIndividual
{
public TabIndividual(MainWindow mainWindow)
public TabIndividual(MainWindow mainWindow, Plugin plugin)
{

if (ImGui.BeginTabItem("Individual"))
Expand All @@ -32,41 +32,76 @@ public TabIndividual(MainWindow mainWindow)
if (ImGui.BeginMenuBar())
{
ImGui.BeginDisabled(!(Plugin.TargetManager.Target != null && Plugin.TargetManager.Target.ObjectKind == ObjectKind.Player));
if (ImGui.MenuItem("Add Target"))
//push font to make our menus with FA icons
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.MenuItem(FontAwesomeIcon.UserPlus.ToIconString()))
{
PlayerService.AddPlayer(Plugin.TargetManager.Target);
}
ImGui.PopFont();
if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Add currently targeted player");
}

ImGui.EndDisabled();

if (Plugin.SelectedPlayer is not null)
{
ImGui.BeginDisabled(Plugin.SelectedPlayer.RemoveDisabled);
if (ImGui.MenuItem("Remove Target"))
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.MenuItem(FontAwesomeIcon.UserMinus.ToIconString()))
{
mainWindow.RemovePlayer();
}
ImGui.PopFont();
if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Remove currently opened player");
}
ImGui.EndDisabled();
}

ImGui.EndMenuBar();
ImGui.EndMenuBar();
}

ImGui.EndChild();
ImGui.SameLine();
ImGui.BeginChild(MainWindow.ID_PANEL_RIGHT, new Vector2(0, 0), true);

//initialize right window with menu bar
ImGui.BeginChild(MainWindow.ID_PANEL_RIGHT, new Vector2(0, 0), true, ImGuiWindowFlags.MenuBar);

if (ImGui.BeginMenuBar())
{
//push font to make our menus with FA icons
ImGui.PushFont(UiBuilder.IconFont);
if (ImGui.MenuItem(FontAwesomeIcon.Save.ToIconString()))
{
FileService.OpenFileDialog(plugin, Plugin.SelectedPlayer.Name);

}
ImGui.PopFont();

if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Save log to .txt file");
}
ImGui.EndMenuBar();
}
ImGui.EndChild();


//Populating selectable list
//Reopen left window, populate selectable list
foreach (var p in Plugin.Players)
{
ImGui.BeginChild(MainWindow.ID_PANEL_LEFT);
mainWindow.AddPlayerSelectable(p);
ImGui.EndChild();
}

// Build the chat log
// Reopen right window, build the chat log
// it's worth noting all of this stuff stays in memory and is only hidden when it's "closed"
ImGui.BeginChild(MainWindow.ID_PANEL_RIGHT);

ImGui.BeginGroup();

if (Plugin.SelectedPlayer is not null)
Expand Down

0 comments on commit 8c71ae3

Please sign in to comment.