Bookmarks

This commit is contained in:
Kilian Hofmann 2023-11-26 09:39:34 +01:00
parent 34780db289
commit 226eb242e3
18 changed files with 441 additions and 200 deletions

View File

@ -1,6 +1 @@
/// <reference types="@microsoft/msfs-types/js/common.d.ts" />
/// <reference types="@microsoft/msfs-types/pages/vcockpit/instruments/shared/baseinstrument.d.ts" />
/// <reference types="@microsoft/msfs-types/js/datastorage.d.ts" />
/// <reference types="@microsoft/msfs-types/js/buttons.d.ts" />
/// <reference types="@microsoft/msfs-types/js/services/toolbarpanels.d.ts" />
/// <reference types="@microsoft/msfs-types/js/simvar.d.ts" />

View File

@ -2,4 +2,5 @@ export const COMMANDS = 'KHOFMANN_PDF_READER_COMMANDS';
export const DATA = 'KHOFMANN_PDF_READER_DATA';
export const LIST = 'LIST';
export const LOAD = 'LOAD';
export const SAVE = 'SAVE';
export const MAX_LIST = 10;

View File

@ -1,12 +1,26 @@
import { FC, PropsWithChildren, createContext, useCallback, useContext, useEffect, useState } from 'react';
import { COMMANDS, DATA, LIST, LOAD } from '../constants';
import { COMMANDS, DATA, LIST, LOAD, SAVE } from '../constants';
interface IDataContext {
list?: IList[];
file?: string;
bookMarks?: IBookMark[];
isLoading?: boolean;
isSaving?: boolean;
refresh?: (path?: string) => void;
load?: (path: string) => void;
addBookMark?: (path: string, bookMarks: IBookMark) => void;
removeBookMark?: (path: string, index: number) => void;
}
interface IData {
file: string;
bookMarks?: IBookMark[];
}
interface IJSONResponse {
id: typeof LIST | typeof LOAD | typeof SAVE;
data: IList[] | IData;
}
export interface IList {
@ -17,14 +31,43 @@ export interface IList {
pages?: number;
}
export interface IBookMark {
page: number;
title: string;
}
export const DataContext = createContext<IDataContext>({});
export const useData = () => {
const { list, file, isLoading, refresh, load } = useContext(DataContext);
const { list, file, bookMarks, isLoading, isSaving, refresh, load, addBookMark, removeBookMark } =
useContext(DataContext);
if (list !== undefined && file !== undefined && isLoading !== undefined && refresh && load)
return { list, file, isLoading, refresh, load };
else throw new Error("Couldn't find context. Is your component inside a DataProvider?");
if (
list !== undefined &&
file !== undefined &&
bookMarks !== undefined &&
isLoading !== undefined &&
isSaving !== undefined &&
refresh &&
load &&
addBookMark &&
removeBookMark
)
return { list, file, bookMarks, isLoading, isSaving, refresh, load, addBookMark, removeBookMark };
else
throw new Error(
`Couldn't find context. Is your component inside a DataProvider? ${JSON.stringify({
list: list !== undefined,
file: file !== undefined,
bookMarks: bookMarks !== undefined,
isLoading: isLoading !== undefined,
isSaving: isSaving !== undefined,
refresh: refresh !== undefined,
load: load !== undefined,
addBookMark: addBookMark !== undefined,
removeBookMark: removeBookMark !== undefined,
})}`
);
};
interface IDataContextProps {
@ -34,7 +77,9 @@ interface IDataContextProps {
const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataListener, children }) => {
const [list, setList] = useState<IList[]>([]);
const [file, setFile] = useState('');
const [bookMarks, setBookMarks] = useState<IBookMark[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
dataListener.on(DATA, dataCallback);
@ -46,17 +91,24 @@ const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataLis
const dataCallback = useCallback((args: string) => {
try {
const json = JSON.parse(args);
const json: IJSONResponse = JSON.parse(args);
console.log(json);
switch (json.id) {
case LIST:
setList((json.data as IList[]).sort((a, b) => a.name.localeCompare(b.name)));
window.dispatchEvent(new Event('resize'));
setList((json.data as IList[]).sort((a, b) => a.name.localeCompare(b.name)) ?? []);
break;
case LOAD:
setFile(json.data);
window.dispatchEvent(new Event('resize'));
const d = json.data as IData;
setFile(d.file);
if (d.bookMarks) {
setBookMarks(d.bookMarks);
}
break;
case SAVE:
setIsSaving(false);
break;
}
window.dispatchEvent(new Event('resize'));
} catch (e) {
console.warn(e);
}
@ -73,7 +125,36 @@ const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataLis
dataListener.call('COMM_BUS_WASM_CALLBACK', COMMANDS, JSON.stringify({ cmd: LOAD, file: path }));
};
return <DataContext.Provider value={{ list, file, isLoading, refresh, load }}>{children}</DataContext.Provider>;
const save = (path: string, bookMarks: IBookMark[]) => {
setIsSaving(true);
dataListener.call('COMM_BUS_WASM_CALLBACK', COMMANDS, JSON.stringify({ cmd: SAVE, path, bookMarks: bookMarks }));
};
const addBookMark = (path: string, bookMark: IBookMark) => {
setBookMarks((prev) => {
const _new = [...prev, bookMark];
_new.sort((a, b) => a.page - b.page);
save(path, _new);
return _new;
});
};
const removeBookMark = (path: string, index: number) => {
setBookMarks((prev) => {
prev.splice(index, 1);
prev.sort((a, b) => a.page - b.page);
save(path, prev);
return [...prev];
});
};
return (
<DataContext.Provider
value={{ list, file, bookMarks, isLoading, isSaving, refresh, load, addBookMark, removeBookMark }}
>
{children}
</DataContext.Provider>
);
};
export default DataContextProvider;

View File

@ -25,7 +25,13 @@ const ThemeProvider: FC<PropsWithChildren> = ({ children }) => {
main: getComputedStyle(document.documentElement).getPropertyValue('--enabledBackGroundColor').trim(),
contrastText: '#fff',
},
secondary: {
main: getComputedStyle(document.documentElement)
.getPropertyValue('--backgroundColorContrastedPanel')
.trim(),
},
},
screenHeight: Number(getComputedStyle(document.documentElement).getPropertyValue('--screenHeight').trim()),
}),
[]
);

View File

@ -170,7 +170,9 @@ const ListPage: FC = () => {
)}
<Grid item xs={11} padding={0}>
<Box display="flex" alignItems="center" height="100%">
<Typography variant="h5">{entry.name}</Typography>
<Typography variant="h5" overflow="ellipsis">
{entry.name}
</Typography>
</Box>
</Grid>
{entry.type === 'directory' && (

View File

@ -1,7 +1,10 @@
import BackIcon from '@mui/icons-material/ArrowBack';
import BookmarkIcon from '@mui/icons-material/Bookmark';
import BookmarkBorderIcon from '@mui/icons-material/BookmarkBorder';
import DeleteIcon from '@mui/icons-material/Delete';
import NavigateBeforeIcon from '@mui/icons-material/NavigateBefore';
import NavigateNextIcon from '@mui/icons-material/NavigateNext';
import { AppBar, Backdrop, Box, Button, TextField, Toolbar, Typography } from '@mui/material';
import { AppBar, Backdrop, Box, Button, Grid, Stack, TextField, Toolbar, Typography } from '@mui/material';
import { FC, useEffect, useRef, useState } from 'react';
import { v4 } from 'uuid';
import { IList, useData } from '../../contexts/DataContext';
@ -10,17 +13,22 @@ import { useRouter } from '../../routers/Router';
const PDFPage: FC = () => {
const { goBack, getProps } = useRouter();
const { path, entry } = getProps() as { path: string; entry: IList };
const { file, isLoading, load } = useData();
const { file, bookMarks, isLoading, isSaving, load, addBookMark, removeBookMark } = useData();
const guid = useRef(v4());
const guid1 = useRef(v4());
const guid2 = useRef(v4());
const [currentPage, setCurrentPage] = useState(1);
const [pageJump, setPageJump] = useState('1');
const [drawerOpen, setDrawerOpen] = useState(false);
const [markName, setMarkName] = useState('');
useEffect(() => {
load(`${path}/${entry.name}/${currentPage}.bjpg`);
}, [currentPage]);
// () =>
return (
<Box>
<AppBar position="sticky">
@ -41,8 +49,8 @@ const PDFPage: FC = () => {
e.stopPropagation();
setPageJump(e.target.value);
}}
onFocus={() => Coherent.trigger('FOCUS_INPUT_FIELD', guid, '', '', '', false)}
onBlur={() => Coherent.trigger('UNFOCUS_INPUT_FIELD', guid)}
onFocus={() => Coherent.trigger('FOCUS_INPUT_FIELD', guid1, '', '', '', false)}
onBlur={() => Coherent.trigger('UNFOCUS_INPUT_FIELD', guid1)}
/>
<Button
onClick={() => {
@ -70,6 +78,13 @@ const PDFPage: FC = () => {
<Button onClick={() => setCurrentPage((prev) => (prev < (entry.pages ?? 0) ? prev + 1 : prev))}>
<NavigateNextIcon htmlColor="white" fontSize="large" />
</Button>
<Button onClick={() => setDrawerOpen(true)}>
{bookMarks.findIndex((mark) => mark.page === currentPage) !== -1 ? (
<BookmarkIcon htmlColor="white" fontSize="large" />
) : (
<BookmarkBorderIcon htmlColor="white" fontSize="large" />
)}
</Button>
</Toolbar>
</AppBar>
{isLoading ? (
@ -81,6 +96,79 @@ const PDFPage: FC = () => {
<img src={`data:image/jpg;base64,${file}`} width="100%" />
</Box>
)}
{drawerOpen && (
<Backdrop
sx={{ color: '#fff', zIndex: (theme) => theme.zIndex.drawer + 1 }}
open
onClick={() => setDrawerOpen(false)}
>
<Box width="33%" position="absolute" right={0} top={0} bottom={0} onClick={(e) => e.stopPropagation()}>
<AppBar position="sticky" sx={{ marginBottom: '44px' }}>
<Toolbar>
<TextField
variant="filled"
value={markName}
sx={{ input: { color: '#fff' }, flexGrow: 1 }}
onChange={(e) => {
e.stopPropagation();
setMarkName(e.target.value);
}}
onFocus={() => Coherent.trigger('FOCUS_INPUT_FIELD', guid2, '', '', '', false)}
onBlur={() => Coherent.trigger('UNFOCUS_INPUT_FIELD', guid2)}
/>
<Button
disabled={isSaving}
onClick={() => {
if (markName !== '') {
addBookMark(`${path}/${entry.name}`, { page: currentPage, title: markName });
setMarkName('');
}
}}
>
<Typography variant="h5" color="#fff">
Add
</Typography>
</Button>
</Toolbar>
</AppBar>
<virtual-scroll
direction="y"
style={{ height: 'calc(100% - 110px)', backgroundColor: 'var(--backgroundColorContrastedPanel)' }}
>
<Stack spacing={2}>
{bookMarks.map((mark, index) => (
<Grid
key={index}
bgcolor="primary.main"
onClick={() => setCurrentPage(mark.page)}
container
padding={1}
height={73}
>
<Grid item xs={9} padding={0}>
<Box display="flex" alignItems="center" height="100%">
<Typography variant="h5" overflow="ellipsis">
{mark.title}
</Typography>
</Box>
</Grid>
<Grid item xs={3} padding={0} display="flex" alignItems="center">
<Button
onClick={(e) => {
e.stopPropagation();
removeBookMark(`${path}/${entry.name}`, index);
}}
>
<DeleteIcon htmlColor="white" fontSize="large" />
</Button>
</Grid>
</Grid>
))}
</Stack>
</virtual-scroll>
</Box>
</Backdrop>
)}
</Box>
);
};

View File

@ -0,0 +1,10 @@
import '@mui/material';
declare module '@mui/material/styles' {
interface Theme {
screenHeight: number;
}
interface ThemeOptions {
screenHeight: number;
}
}

View File

@ -1,11 +1,11 @@
import { DOMAttributes, ReactNode } from 'react';
import { CSSProperties, DOMAttributes, ReactNode } from 'react';
type CustomElement<T> = Partial<T & DOMAttributes<T> & { children: ReactNode }>;
declare global {
namespace JSX {
interface IntrinsicElements {
['virtual-scroll']: CustomElement<{ class?: string; direction: 'x' | 'y' }>;
['virtual-scroll']: CustomElement<{ class?: string; direction: 'x' | 'y'; style?: CSSProperties }>;
}
}
}

View File

@ -1,71 +0,0 @@
#pragma once
/**
* The MIT License (MIT)
* Copyright (c) 2016 tomykaira
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string>
namespace macaron {
class Base64 {
public:
static std::string Encode(const char* data, size_t length) {
static constexpr char sEncodingTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
size_t out_len = 4 * ((length + 2) / 3);
std::string ret(out_len, '\0');
size_t i;
char* p = const_cast<char*>(ret.c_str());
for (i = 0; i < length - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int)(data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
if (i < length) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (length - 1)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
return ret;
}
};
}

View File

@ -1,6 +1,10 @@
#pragma once
#define PACKAGE_DIR "/work/Files/"
#define WORK_DIR "/work/BookMarks/"
#define COMMANDS "KHOFMANN_PDF_READER_COMMANDS"
#define DATA "KHOFMANN_PDF_READER_DATA"
#define MAX_LIST 10
#define LIST "LIST"
#define LOAD "LOAD"
#define SAVE "SAVE"
#define MAX_LIST 10

View File

@ -1,28 +1,22 @@
#pragma once
#include <MSFS\Legacy\gauges.h>
#include <string>
#include <errno.h>
#include <dirent.h>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "Base64.hpp"
#include "Utils.hpp"
#include "Utils.h"
#include "Constants.h"
#include "FileSystem.h"
namespace khofmann
{
/// <summary>
/// Read a file and return file contents
/// </summary>
/// <param name="path">Path of BASE64 encoded File</param>
/// <param name="alloc">Allocator</param>
/// <returns>File contents</returns>
static rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc)
rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc)
{
FILE* file = fopen(path, "r");
if (file) {
if (file)
{
fseek(file, 0, SEEK_END);
long fsize = ftell(file);
fseek(file, 0, SEEK_SET);
@ -36,17 +30,30 @@ namespace khofmann
return retVal;
}
int err = errno;
log(stderr, "Error loading file %s: ERRNO %i\n", path, err);
return rapidjson::Value("", alloc);
}
/// <summary>
/// Enumerate a directory and return list of directories.
/// Return tumbnail as BASE64 and page count if present.
/// </summary>
/// <param name="path">Path of directory</param>
/// <param name="files">Pointer to files value object</param>
void writeFile(const char* path, rapidjson::Value &contents)
{
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
contents.Accept(writer);
FILE* file = fopen(path, "w");
if (file)
{
fwrite(strbuf.GetString(), sizeof(char), strbuf.GetSize(), file);
fclose(file);
return;
}
int err = errno;
log(stderr, "Error saving file %s: ERRNO %i\n", path, err);
}
/// <param name="alloc">Allocator</param>
static void enumerateDir(const char* path, rapidjson::Value *files, rapidjson::Document::AllocatorType& alloc)
void enumerateDir(const char* path, rapidjson::Value& files, rapidjson::Document::AllocatorType& alloc)
{
DIR* d = opendir(path);
if (d)
@ -84,7 +91,12 @@ namespace khofmann
entry.AddMember("pages", count - 1, alloc);
entry.AddMember("thumb", readFile(thumb.c_str(), alloc).Move(), alloc);
entry.AddMember("type", "file", alloc);
files->PushBack(entry.Move(), alloc);
files.PushBack(entry.Move(), alloc);
}
else
{
int err = errno;
log(stderr, "Could not open directory %s: ERRNO %i\n", dirPath.c_str(), err);
}
closedir(f);
}
@ -96,10 +108,15 @@ namespace khofmann
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
entry.AddMember("type", "directory", alloc);
files->PushBack(entry.Move(), alloc);
files.PushBack(entry.Move(), alloc);
}
}
}
else
{
int err = errno;
log(stderr, "Could not open directory %s: ERRNO %i\n", path, err);
}
closedir(d);
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <MSFS/Legacy/gauges.h>
#include "rapidjson/document.h"
namespace khofmann
{
/// <summary>
/// Read a file and return file contents
/// </summary>
/// <param name="path">Path of BASE64 encoded File</param>
/// <param name="alloc">Allocator</param>
/// <returns>File contents</returns>
rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc);
/// <summary>
/// Write a file to disk
/// </summary>
/// <param name="path">Path to file</param>
/// <param name="contents">Contents of file</param>
/// <param name="length">Length of contents</param>
void writeFile(const char* path, rapidjson::Value& contents);
/// <summary>
/// Enumerate a directory and return list of directories.
/// Return tumbnail as BASE64 and page count if present.
/// </summary>
/// <param name="path">Path of directory</param>
/// <param name="files">Pointer to files value object</param>
/// <param name="alloc">Allocator</param>
void enumerateDir(const char* path, rapidjson::Value& files, rapidjson::Document::AllocatorType& alloc);
}

View File

@ -1,55 +1,57 @@
#include <MSFS\MSFS.h>
#include <MSFS\Legacy\gauges.h>
#include <MSFS\MSFS_CommBus.h>
#include <MSFS/MSFS.h>
#include <MSFS/MSFS_CommBus.h>
#include <MSFS/Legacy/gauges.h>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <iterator>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "FileSystem.hpp"
#include "Utils.hpp"
#include "FileSystem.h"
#include "Utils.h"
#include "Constants.h"
static void CommandWasmCallback(const char* args, unsigned int size, void* ctx);
static FILE* logFile;
extern "C"
{
MSFS_CALLBACK void module_init(void)
{
khofmann::log(stdout, "[PDF-Reader Module] Starting init\n");
khofmann::log(stdout, "Starting init\n");
khofmann::logFile = fopen("/work/log.txt", "w");
if (khofmann::logFile == NULL)
{
khofmann::log(stderr, "[PDF-Reader Module] Error creating logfile\n");
khofmann::log(stderr, "Error creating logfile\n");
}
if (!fsCommBusRegister(COMMANDS, CommandWasmCallback))
{
khofmann::log(stderr, "[PDF-Reader Module] Error registering command CommBus\n");
khofmann::log(stderr, "Error registering command CommBus\n");
}
khofmann::log(stdout, "[PDF-Reader Module] Inited\n");
khofmann::log(stdout, "Inited\n");
}
extern "C" MSFS_CALLBACK void module_deinit(void)
{
khofmann::log(stdout, "[PDF-Reader Module] Starting deinit\n");
khofmann::log(stdout, "Starting deinit\n");
int c = fsCommBusUnregister(COMMANDS, CommandWasmCallback);
khofmann::log(stdout, "%i unregistered", &c);
khofmann::log(stdout, "[PDF-Reader Module] Deinited\n");
khofmann::log(stdout, "Deinited\n");
fclose(khofmann::logFile);
}
@ -61,57 +63,87 @@ extern "C"
const char* cmd = inDoc["cmd"].GetString();
if (strcmp(cmd, "LIST") == 0)
rapidjson::Document outDoc;
rapidjson::Document::AllocatorType& allocator = outDoc.GetAllocator();
outDoc.SetObject();
outDoc.AddMember("id", rapidjson::Value(cmd, allocator).Move(), allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
if (strcmp(cmd, LIST) == 0)
{
std::string path;
path += PACKAGE_DIR;
path += inDoc["path"].GetString();
rapidjson::Document outDoc;
rapidjson::Document::AllocatorType& allocator = outDoc.GetAllocator();
outDoc.SetObject();
outDoc.AddMember("id", "LIST", allocator);
rapidjson::Value files;
files.SetArray();
khofmann::enumerateDir(path.c_str(), &files, allocator);
khofmann::enumerateDir(path.c_str(), files, allocator);
outDoc.AddMember("data", files.Move(), allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
outDoc.Accept(writer);
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
}
else
else if (strcmp(cmd, LOAD) == 0)
{
rapidjson::Document inDoc;
inDoc.Parse(args);
std::string filePath;
filePath += PACKAGE_DIR;
filePath += inDoc["file"].GetString();
const char* cmd = inDoc["cmd"].GetString();
if (strcmp(cmd, "LOAD") == 0)
std::string marksPath;
marksPath += inDoc["file"].GetString();
size_t lastSlash = marksPath.find_last_of("/");
if (lastSlash != std::string::npos)
{
std::string path;
path += PACKAGE_DIR;
path += inDoc["file"].GetString();
khofmann::log(stdout, "Loading file %s\n", (void*)path.c_str());
rapidjson::Document outDoc;
rapidjson::Document::AllocatorType& allocator = outDoc.GetAllocator();
outDoc.SetObject();
outDoc.AddMember("id", "LOAD", allocator);
outDoc.AddMember("data", khofmann::readFile(path.c_str(), allocator).Move(), allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
outDoc.Accept(writer);
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
marksPath.erase(lastSlash);
}
marksPath.erase(std::remove(marksPath.begin(), marksPath.end(), '/'), marksPath.end());
marksPath = WORK_DIR + marksPath + "_bookmarks.json";
rapidjson::Value data;
data.SetObject();
khofmann::log(stdout, "Loading file %s\n", filePath.c_str());
data.AddMember("file", khofmann::readFile(filePath.c_str(), allocator).Move(), allocator);
if (filePath.find("/1.bjpg") != std::string::npos)
{
khofmann::log(stdout, "Loading book marks %s\n", marksPath.c_str());
rapidjson::Document marks;
marks.Parse(khofmann::readFile(marksPath.c_str(), allocator).GetString());
if (marks.IsArray())
{
data.AddMember("bookMarks", marks.Move(), allocator);
}
else
{
rapidjson::Value t;
t.SetArray();
data.AddMember("bookMarks", t.Move(), allocator);
}
}
outDoc.AddMember("data", data.Move(), allocator);
outDoc.Accept(writer);
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
}
else if (strcmp(cmd, SAVE) == 0)
{
std::string marksPath;
marksPath += inDoc["path"].GetString();
marksPath.erase(std::remove(marksPath.begin(), marksPath.end(), '/'), marksPath.end());
marksPath = WORK_DIR + marksPath + "_bookmarks.json";
khofmann::log(stdout, "Saving book marks %s\n", marksPath.c_str());
khofmann::writeFile(marksPath.c_str(), inDoc["bookMarks"]);
outDoc.Accept(writer);
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
}
}
}

View File

@ -116,16 +116,17 @@
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="FileSystem.hpp" />
<ClCompile Include="FileSystem.cpp" />
<ClCompile Include="Module.cpp" />
<ClCompile Include="Utils.hpp">
<ClCompile Include="Utils.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|MSFS'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|MSFS'">false</ExcludedFromBuild>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Base64.hpp" />
<ClInclude Include="Constants.h" />
<ClInclude Include="FileSystem.h" />
<ClInclude Include="Utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -2,11 +2,23 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="Module.cpp" />
<ClCompile Include="FileSystem.hpp" />
<ClCompile Include="Utils.hpp" />
<ClCompile Include="FileSystem.cpp" />
<ClCompile Include="Utils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Base64.hpp" />
<ClInclude Include="Constants.h" />
<ClInclude Include="FileSystem.h">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="Utils.h">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="Constants.h">
<Filter>Headers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Headers">
<UniqueIdentifier>{cc9ebc6c-fa76-4cce-aad2-8c91df6e65c1}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>

View File

@ -0,0 +1,41 @@
#include <cstdarg>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Utils.h"
namespace khofmann
{
void log(FILE* stream, const char* format, ...)
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = gmtime(&rawtime);
char fmt[256];
char time[256];
strcpy(time, asctime(timeinfo));
time[strlen(time) - 1] = 0;
sprintf(fmt, "[PDF - Reader Module] %s - %s", time, format);
va_list args;
va_start(args, format);
if (logFile != NULL)
{
vfprintf(logFile, fmt, args);
fflush(logFile);
}
vfprintf(stream, fmt, args);
va_end(args);
//free(fmt);
}
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <MSFS/Legacy/gauges.h>
namespace khofmann
{
extern FILE* logFile;
/// <summary>
/// Write to logfile and console
/// </summary>
/// <param name="stream">Console stream</param>
/// <param name="format">Format string</param>
void log(FILE* stream, const char* format, ...);
}

View File

@ -1,26 +0,0 @@
#pragma once
#include <MSFS\Legacy\gauges.h>
#include <stdio.h>
namespace khofmann
{
static FILE* logFile;
/// <summary>
/// Write to logfile and console
/// </summary>
/// <param name="stream">Console stream</param>
/// <param name="format">Format string wiht up to one specifier</param>
/// <param name="optionalElement">Optional element specified by specifier</param>
static void log(FILE* stream, const char* format, void* optionalElement = NULL)
{
if (logFile != NULL)
{
fprintf(logFile, format, optionalElement);
fflush(logFile);
}
fprintf(stream, format, optionalElement);
}
}