This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
147 lines (123 loc) · 5.3 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace MusicBeePlugin
{
public partial class Plugin
{
private MusicBeeApiInterface _mbApiInterface;
private PluginInfo about = new PluginInfo();
private Settings _settingsWindow;
private int _maxEntries;
private string _outputPath;
private string _path;
private ConfigMgr _configMgr;
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
_mbApiInterface = new MusicBeeApiInterface();
_mbApiInterface.Initialise(apiInterfacePtr);
_path = _mbApiInterface.Setting_GetPersistentStoragePath() + "mb_SongHistoryConfig.xml";
about.PluginInfoVersion = PluginInfoVersion;
about.Name = "mb_SongHistory";
about.Description = "This plugin enables you to record your play history in a text file.";
about.Author = "Zkhcohen";
about.TargetApplication = ""; // the name of a Plugin Storage device or panel header for a dockable panel
about.Type = PluginType.General;
about.VersionMajor = 1; // your plugin version
about.VersionMinor = 0;
about.Revision = 1;
about.MinInterfaceVersion = MinInterfaceVersion;
about.MinApiRevision = MinApiRevision;
about.ReceiveNotifications = (ReceiveNotificationFlags.PlayerEvents | ReceiveNotificationFlags.TagEvents);
about.ConfigurationPanelHeight = 0; // height in pixels that musicbee should reserve in a panel for config settings. When set, a handle to an empty panel will be passed to the Configure function
return about;
}
public bool Configure(IntPtr panelHandle)
{
// save any persistent settings in a sub-folder of this path
string dataPath = _mbApiInterface.Setting_GetPersistentStoragePath();
// panelHandle will only be set if you set about.ConfigurationPanelHeight to a non-zero value
// keep in mind the panel width is scaled according to the font the user has selected
// if about.ConfigurationPanelHeight is set to 0, you can display your own popup window
_settingsWindow = new Settings(_path);
_settingsWindow.SetText();
_settingsWindow.ShowDialog();
return true;
}
// called by MusicBee when the user clicks Apply or Save in the MusicBee Preferences screen.
// its up to you to figure out whether anything has changed and needs updating
public void SaveSettings()
{
SetVariables();
}
// MusicBee is closing the plugin (plugin is being disabled by user or MusicBee is shutting down)
public void Close(PluginCloseReason reason)
{
}
// uninstall this plugin - clean up any persisted files
public void Uninstall()
{
}
private void GetSettings()
{
_configMgr = new ConfigMgr();
if (!File.Exists(_path))
{
File.Create(_path).Close();
_configMgr.SetDefaults(_path);
MessageBox.Show("Please configure mb_SongHistory in the Preferences > Plugins menu, then restart MusicBee.");
}
else
{
SetVariables();
}
}
private void SetVariables()
{
var deserializedObject = _configMgr.DeserializeConfig(_path);
_outputPath = deserializedObject.OutputPath;
_maxEntries = Convert.ToInt16(deserializedObject.MaxEntries);
}
private string Entry()
{
string time = DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss");
string title = _mbApiInterface.NowPlaying_GetFileTag(MetaDataType.TrackTitle);
string artist = _mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Artist);
return $"{time} - {title} - {artist}";
}
private void WriteEntry()
{
//MessageBox.Show("Writing Entries.");
if (!File.Exists(_outputPath))
{
File.Create(_outputPath).Close();
}
var lineCount = File.ReadLines(_outputPath).Count();
if (_maxEntries != 0 && lineCount >= _maxEntries)
{
var lines = File.ReadAllLines(_outputPath).Skip((lineCount + 1) - _maxEntries);
File.WriteAllLines(_outputPath, lines);
}
using (StreamWriter sw = new StreamWriter(_outputPath, true))
{
sw.WriteLine(Entry());
}
}
// receive event notifications from MusicBee
// you need to set about.ReceiveNotificationFlags = PlayerEvents to receive all notifications, and not just the startup event
public void ReceiveNotification(string sourceFileUrl, NotificationType type)
{
// perform some action depending on the notification type
switch (type)
{
case NotificationType.PluginStartup:
GetSettings();
break;
case NotificationType.TrackChanged:
WriteEntry();
break;
}
}
}
}