import { ApplicationConfigState , ApplicationImpl , DatetimeProjectItemImpl , ElectronExecutor , Group , GroupName , InputSettingItem , Platform , SettingItem , } from '../../../Types' ; import { SqliteBrowserApplicationImpl } from '../index' ; import { execFileSync } from 'child_process' ; import { isEmpty , unique } from 'licia' ; import { removeAllQueryFromUrl , systemHome } from '../../../Utils' ; import { Context } from '../../../Context' ; import { existsSync } from 'fs' ; import { i18n , sentenceKey } from '../../../i18n' ; import { generatePinyinIndex } from '../../../utils/index-generator/PinyinIndex' ; import { generateHostIndex } from '../../../utils/index-generator/HostIndex' ; import { parseSqliteDefaultResult } from '../../../utils/sqlite/ParseResult' ; const SAFARI = 'safari' ; export class SafariHistoryProjectItemImpl extends DatetimeProjectItemImpl { } export class SafariHistoryApplicationImpl extends SqliteBrowserApplicationImpl < SafariHistoryProjectItemImpl > { constructor ( ) { super ( `${ SAFARI }-history` , 'Safari' , `icon/browser-${ SAFARI }.png` , SAFARI , [ Platform . darwin ] , Group [ GroupName . browserHistory ] , undefined , false , 'History.db' ) ; } override defaultConfigPath ( ) { return `${ systemHome ( ) }/Library/Safari/History.db` ; } async generateCacheProjectItems ( context ) { let items = [ ] ; let configPath = `${ utools . getPath ( 'home' ) }/Library/Safari/History.db` ; if ( ! existsSync ( configPath ) ) { return [ ] ; } let sql = 'select i.url                                                                                         as url,\n       v.title                                                                                       as title,\n       cast(strftime(\'%s\', datetime(v.visit_time + 978307200, \'unixepoch\', \'localtime\')) as numeric) as timestamp\nfrom history_items i,\n     history_visits v\nwhere i.id = v.history_item\ngroup by i.url\norder by timestamp desc\nlimit ' + context . browserHistoryLimit ; let result = '' ; await this . copyAndReadFile ( configPath , path => { result = execFileSync ( this . executor , [ path , sql , '-readonly' ] , { encoding : 'utf-8' , maxBuffer : 20971520 } ) ; } ) ; if ( ! isEmpty ( result ) ) { let array = parseSqliteDefaultResult ( result , [ 'url' , 'title' , 'n/timestamp' ] ) ; array . forEach ( i => { let title = i [ 'title' ] ?? '' ; let url = i [ 'url' ] ?? '' ; items . push ( { id : '' , title : title , description : url , icon : this . ifGetFavicon ( removeAllQueryFromUrl ( url ) , context ) , searchKey : unique ( [ ... generatePinyinIndex ( context , title ) , ... generateHostIndex ( context , url ) , title , ] ) , exists : true , command : new ElectronExecutor ( url ) , datetime : i [ 'timestamp' ] ?? 0 , } ) ; } ) ; } return items ; } override generateSettingItems ( context , nativeId ) { return [ new InputSettingItem ( this . executorId ( nativeId ) , i18n . t ( sentenceKey . sqlite3 ) , this . executor , i18n . t ( sentenceKey . sqlite3Desc ) ) , ] ; } override isFinishConfig ( ) { if ( this . disEnable ( ) ) return ApplicationConfigState . empty ; return this . executor ? ApplicationConfigState . done : ApplicationConfigState . undone ; } } export const applications = [ new SafariHistoryApplicationImpl ( ) , ] ;