Skip to content

Commit

Permalink
Merge pull request #1 from jamesf91/feature/ssh-datasource
Browse files Browse the repository at this point in the history
Feature/ssh datasource
  • Loading branch information
jamesf91 authored Mar 26, 2021
2 parents 1049b5e + 30cbca2 commit d572cc4
Show file tree
Hide file tree
Showing 19 changed files with 1,384 additions and 670 deletions.
20 changes: 16 additions & 4 deletions ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,36 @@ class Program
{
static void Main(string[] args)
{
RmCloud cloud = new RmCloud();
string settingsRegPath = @"Software\Microsoft\Office\OneNote\AddInsData\RemarkableSync.OnenoteAddin";
IConfigStore _configStore = new WinRegistryConfigStore(settingsRegPath);

List<RmItem> rootItems = cloud.GetItemHierarchy().Result;
// setup
Dictionary<string, string> mapConfigs = new Dictionary<string, string>();
mapConfigs["sshHost"] = "10.11.99.1";
mapConfigs["SshPassword"] = "ABvontxEol";
_configStore.SetConfigs(mapConfigs);

// end setup

IRmDataSource dataSource = new RmSftpDataSource(_configStore);
//IRmDataSource dataSource = new RmCloudDataSource(_configStore);

List<RmItem> rootItems = dataSource.GetItemHierarchy().Result;
RmItem item = (from root in rootItems
where root.Type == RmItem.DocumentType
select root).ToArray()[0];

List<RmPage> pages = new List<RmPage>();

using (RmDownloadedDoc doc = cloud.DownloadDocument(item).Result)
using (RmDownloadedDoc doc = dataSource.DownloadDocument(item).Result)
{
for (int i = 0; i < doc.PageCount; ++i)
{
pages.Add(doc.GetPageContent(i));
}
}

MyScriptClient hwrClient = new MyScriptClient();
MyScriptClient hwrClient = new MyScriptClient(_configStore);
MyScriptResult result = hwrClient.RequestHwr(pages).Result;
if (result != null)
{
Expand Down
6 changes: 4 additions & 2 deletions OnenoteAddin/AddIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class AddIn : IDTExtensibility2, IRibbonExtensibility
protected Application OneNoteApplication
{ get; set; }

private const string _settingsRegPath = @"Software\Microsoft\Office\OneNote\AddInsData\RemarkableSync.OnenoteAddin";

private RmDownloadForm _downloadForm;
private SettingsForm _settingForm;
private Thread _downloadFormThread;
Expand Down Expand Up @@ -179,7 +181,7 @@ public async Task onSettingsClicked(IRibbonControl control)
private void ShowDownloadForm(dynamic owner)
{
System.Windows.Forms.Application.EnableVisualStyles();
_downloadForm = new RmDownloadForm(OneNoteApplication);
_downloadForm = new RmDownloadForm(OneNoteApplication, _settingsRegPath);
_downloadForm.Visible = false;
_downloadForm.ShowDialog(owner);
_downloadForm = null;
Expand All @@ -189,7 +191,7 @@ private void ShowDownloadForm(dynamic owner)
private void ShowSettingsForm(dynamic owner)
{
System.Windows.Forms.Application.EnableVisualStyles();
_settingForm = new SettingsForm();
_settingForm = new SettingsForm(_settingsRegPath);
_settingForm.Visible = false;
_settingForm.ShowDialog(owner);
_settingForm = null;
Expand Down
1 change: 1 addition & 0 deletions OnenoteAddin/RmDownloadForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 65 additions & 15 deletions OnenoteAddin/RmDownloadForm.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using RemarkableSync.RmLine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -46,23 +40,65 @@ public static List<RmTreeNode> FromRmItem(List<RmItem> rmItems)
}
}

private RmCloud _rmCloudClient;
private IRmDataSource _rmDataSource;
private Application _application;
private IConfigStore _configStore;

public RmDownloadForm(Application application)
public RmDownloadForm(Application application, string settingsRegPath)
{
_rmCloudClient = new RmCloud();
_configStore = new WinRegistryConfigStore(settingsRegPath);
_application = application;

InitializeComponent();
InitializeData();
InitializeData();
}

private async void InitializeData()
{
rmTreeView.Nodes.Clear();
lblInfo.Text = "Loading document list from reMarkable...";

var rootItems = await _rmCloudClient.GetItemHierarchy();
List<RmItem> rootItems = new List<RmItem>();

try
{
await Task.Run(() =>
{
int connMethod = -1;
try
{
string connMethodString = _configStore.GetConfig(SettingsForm.RmConnectionMethodConfig);
connMethod = Convert.ToInt32(connMethodString);
}
catch (Exception err)
{
Console.WriteLine($"RmDownloadForm::RmDownloadForm() - Failed to get RmConnectionMethod config with err: {err.Message}");
// will default to cloud
}

switch (connMethod)
{
case (int)SettingsForm.RmConnectionMethod.Ssh:
_rmDataSource = new RmSftpDataSource(_configStore);
Console.WriteLine("Using SFTP data source");
break;
case (int)SettingsForm.RmConnectionMethod.RmCloud:
default:
_rmDataSource = new RmCloudDataSource(_configStore);
Console.WriteLine("Using rm cloud data source");
break;
}
});
rootItems = await _rmDataSource.GetItemHierarchy();
}
catch (Exception err)
{
Console.WriteLine($"Error getting notebook structure from reMarkable. Err: {err.Message}");
MessageBox.Show($"Error getting notebook structure from reMarkable.\n{err.Message}", "Error");
Close();
return;
}

Console.WriteLine("Got item hierarchy from remarkable cloud");
var treeNodeList = RmTreeNode.FromRmItem(rootItems);

Expand All @@ -88,8 +124,18 @@ private async void btnOk_Click(object sender, EventArgs e)
RmTreeNode rmTreeNode = (RmTreeNode) rmTreeView.SelectedNode;
Console.WriteLine($"Selected: {rmTreeNode.VisibleName} | {rmTreeNode.ID}");

bool success = await ImportDocument(rmTreeNode);
Console.WriteLine("Import " + (success ? "successful" : "failed"));
try
{
bool success = await ImportDocument(rmTreeNode);
Console.WriteLine("Import " + (success ? "successful" : "failed"));
}
catch (Exception err)
{
Console.WriteLine($"Error importing document from reMarkable. Err: {err.Message}");
MessageBox.Show($"Error importing document from reMarkable.\n{err.Message}", "Error");
Close();
return;
}
}


Expand All @@ -110,7 +156,7 @@ private async Task<bool> ImportDocument(RmTreeNode rmTreeNode)

lblInfo.Text = $"Downloading {rmTreeNode.VisibleName}...";

using (RmDownloadedDoc doc = await _rmCloudClient.DownloadDocument(item))
using (RmDownloadedDoc doc = await _rmDataSource.DownloadDocument(item))
{
Console.WriteLine("ImportDocument() - document downloaded");
for (int i = 0; i < doc.PageCount; ++i)
Expand All @@ -120,7 +166,7 @@ private async Task<bool> ImportDocument(RmTreeNode rmTreeNode)
}

lblInfo.Text = $"Digitising {rmTreeNode.VisibleName}...";
MyScriptClient hwrClient = new MyScriptClient();
MyScriptClient hwrClient = new MyScriptClient(_configStore);
Console.WriteLine("ImportDocument() - requesting hand writing recognition");
MyScriptResult result = await hwrClient.RequestHwr(pages);

Expand Down Expand Up @@ -149,5 +195,9 @@ private void UpdateOneNoteWithHwrResult(string name, MyScriptResult result)
oneNoteHelper.AddPageContent(newPageId, result.label);
}

private void RmDownloadForm_FormClosing(object sender, FormClosingEventArgs e)
{
_rmDataSource?.Dispose();
}
}
}
Loading

0 comments on commit d572cc4

Please sign in to comment.