2017-04-16 03:15:35 +09:00
import { app , systemPreferences , dialog , shell } from 'electron' ;
2017-04-15 16:32:44 +09:00
import * as fs from 'fs' ;
import log from './log' ;
2017-04-19 20:56:54 +09:00
import { CONFIG_FILE , IS_DARWIN , IS_WINDOWS } from './common' ;
2017-04-15 16:32:44 +09:00
export interface Account {
host : string ;
name : string ;
2017-04-15 17:05:32 +09:00
default_page : string ;
2017-04-15 16:32:44 +09:00
}
export interface Config {
hot_key : string ;
icon_color : string ;
always_on_top : boolean ;
2017-04-19 16:39:54 +02:00
menubar : boolean ;
2017-04-15 16:32:44 +09:00
normal_window : boolean ;
zoom_factor : number ;
accounts : Account [ ] ;
keymaps : { [ key : string ] : string } ;
}
function makeDefaultConfig ( ) : Config {
2017-04-19 20:56:54 +09:00
const IsDarkMode = IS_DARWIN && systemPreferences . isDarkMode ( ) ;
const menubarBroken = IS_WINDOWS ;
2017-04-15 16:32:44 +09:00
return {
hot_key : 'CmdOrCtrl+Shift+S' ,
icon_color : IsDarkMode ? 'white' : 'black' ,
always_on_top : false ,
2017-04-19 16:39:54 +02:00
menubar : true ,
2017-04-15 16:32:44 +09:00
normal_window : menubarBroken ,
2017-04-16 03:15:35 +09:00
zoom_factor : 0.9 ,
accounts : [ {
name : '' ,
host : '' ,
default_page : '/web/timelines/home' ,
} ] ,
2017-04-16 04:14:58 +09:00
keymaps : {
j : 'scroll-down' ,
k : 'scroll-up' ,
i : 'scroll-top' ,
m : 'scroll-bottom' ,
2017-04-16 22:43:04 +09:00
1 : '/web/statuses/new' ,
2 : '/web/timelines/home' ,
3 : '/web/notifications' ,
4 : '/web/timelines/public/local' ,
5 : '/web/timelines/public' ,
6 : '/web/getting-started'
2017-04-16 04:14:58 +09:00
} ,
2017-04-15 16:32:44 +09:00
} ;
}
2017-04-16 03:15:35 +09:00
function showDyingDialog ( title : string , detail : string ) {
dialog . showMessageBox ( {
type : 'info' ,
message : title ,
detail ,
} , ( ) = > {
app . quit ( ) ;
} ) ;
}
function recommendConfigAndDie ( file : string ) {
2017-04-16 12:44:53 +09:00
const title = 'Please write configuration in JSON' ;
2017-04-16 03:15:35 +09:00
const detail = 'You need to write up name and host in first item of accounts. Restart this app after writing up them. Please see README for more detail: https://github.com/rhysd/Mstdn#readme' ;
shell . openItem ( file ) ;
showDyingDialog ( title , detail ) ;
}
2017-04-15 16:32:44 +09:00
export default function loadConfig ( ) : Promise < Config > {
return new Promise < Config > ( resolve = > {
2017-04-19 16:13:33 +09:00
fs . readFile ( CONFIG_FILE , 'utf8' , ( err , json ) = > {
2017-04-15 16:32:44 +09:00
if ( err ) {
2017-04-19 16:13:33 +09:00
log . info ( 'Configuration file was not found, will create:' , CONFIG_FILE ) ;
2017-04-15 16:32:44 +09:00
const default_config = makeDefaultConfig ( ) ;
// Note:
// If calling writeFile() directly here, it tries to create config file before Electron
// runtime creates data directory. As the result, writeFile() would fail to create a file.
if ( app . isReady ( ) ) {
2017-04-19 16:13:33 +09:00
fs . writeFileSync ( CONFIG_FILE , JSON . stringify ( default_config , null , 2 ) ) ;
recommendConfigAndDie ( CONFIG_FILE ) ;
2017-04-15 16:32:44 +09:00
} else {
2017-04-16 03:15:35 +09:00
app . once ( 'ready' , ( ) = > {
2017-04-19 16:13:33 +09:00
fs . writeFileSync ( CONFIG_FILE , JSON . stringify ( default_config , null , 2 ) ) ;
recommendConfigAndDie ( CONFIG_FILE ) ;
2017-04-16 03:15:35 +09:00
} ) ;
2017-04-15 16:32:44 +09:00
}
2017-04-16 03:15:35 +09:00
return ;
2017-04-15 16:32:44 +09:00
}
try {
const config = JSON . parse ( json ) ;
if ( config . hot_key && config . hot_key . startsWith ( 'mod+' ) ) {
config . hot_key = ` CmdOrCtrl+ ${ config . hot_key . slice ( 4 ) } ` ;
}
log . debug ( 'Configuration was loaded successfully' , config ) ;
2017-04-16 03:15:35 +09:00
if ( ! config . accounts || config . accounts [ 0 ] . host === '' || config . accounts [ 0 ] . name === '' ) {
2017-04-19 16:13:33 +09:00
recommendConfigAndDie ( CONFIG_FILE ) ;
2017-04-16 03:15:35 +09:00
} else {
resolve ( config ) ;
}
2017-04-15 16:32:44 +09:00
} catch ( e ) {
2017-04-16 03:15:35 +09:00
log . debug ( 'Error on loading JSON file' , e ) ;
2017-04-16 12:44:53 +09:00
showDyingDialog ( 'Error on loading JSON file' , e . message ) ;
2017-04-15 16:32:44 +09:00
}
} ) ;
} ) ;
}