profiles.service.ts 6.6 KB
Newer Older
E
Eugene Pankov 已提交
1 2 3 4
import { Injectable, Inject } from '@angular/core'
import { NewTabParameters } from './tabs.service'
import { BaseTabComponent } from '../components/baseTab.component'
import { Profile, ProfileProvider } from '../api/profileProvider'
E
Eugene Pankov 已提交
5
import { SelectorOption } from '../api/selector'
E
Eugene Pankov 已提交
6
import { AppService } from './app.service'
7
import { configMerge, ConfigProxy, ConfigService } from './config.service'
8 9
import { NotificationsService } from './notifications.service'
import { SelectorService } from './selector.service'
E
Eugene Pankov 已提交
10 11 12

@Injectable({ providedIn: 'root' })
export class ProfilesService {
13 14 15 16 17 18 19 20 21 22 23 24 25 26
    private profileDefaults = {
        id: '',
        type: '',
        name: '',
        group: '',
        options: {},
        icon: '',
        color: '',
        disableDynamicTitle: false,
        weight: 0,
        isBuiltin: false,
        isTemplate: false,
    }

E
Eugene Pankov 已提交
27 28 29
    constructor (
        private app: AppService,
        private config: ConfigService,
30 31
        private notifications: NotificationsService,
        private selector: SelectorService,
E
Eugene Pankov 已提交
32 33 34 35
        @Inject(ProfileProvider) private profileProviders: ProfileProvider[],
    ) { }

    async openNewTabForProfile (profile: Profile): Promise<BaseTabComponent|null> {
36
        profile = this.getConfigProxyForProfile(profile)
E
Eugene Pankov 已提交
37 38 39 40
        const params = await this.newTabParametersForProfile(profile)
        if (params) {
            const tab = this.app.openNewTab(params)
            ;(this.app.getParentTab(tab) ?? tab).color = profile.color ?? null
41
            tab.setTitle(profile.name)
E
Eugene Pankov 已提交
42 43 44 45 46 47 48 49 50
            if (profile.disableDynamicTitle) {
                tab['enableDynamicTitle'] = false
            }
            return tab
        }
        return null
    }

    async newTabParametersForProfile (profile: Profile): Promise<NewTabParameters<BaseTabComponent>|null> {
51
        profile = this.getConfigProxyForProfile(profile)
E
Eugene Pankov 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
        return this.providerForProfile(profile)?.getNewTabParameters(profile) ?? null
    }

    getProviders (): ProfileProvider[] {
        return [...this.profileProviders]
    }

    async getProfiles (): Promise<Profile[]> {
        const lists = await Promise.all(this.config.enabledServices(this.profileProviders).map(x => x.getBuiltinProfiles()))
        let list = lists.reduce((a, b) => a.concat(b), [])
        list = [
            ...this.config.store.profiles ?? [],
            ...list,
        ]
66 67
        const sortKey = p => `${p.group ?? ''} / ${p.name}`
        list.sort((a, b) => sortKey(a).localeCompare(sortKey(b)))
E
Eugene Pankov 已提交
68 69 70 71 72 73 74
        list.sort((a, b) => (a.isBuiltin ? 1 : 0) - (b.isBuiltin ? 1 : 0))
        return list
    }

    providerForProfile (profile: Profile): ProfileProvider|null {
        return this.profileProviders.find(x => x.id === profile.type) ?? null
    }
E
Eugene Pankov 已提交
75 76

    selectorOptionForProfile <T> (profile: Profile): SelectorOption<T> {
E
WSA wip  
Eugene Pankov 已提交
77
        profile = this.getConfigProxyForProfile(profile)
E
Eugene Pankov 已提交
78 79 80 81 82 83
        return {
            icon: profile.icon,
            name: profile.group ? `${profile.group} / ${profile.name}` : profile.name,
            description: this.providerForProfile(profile)?.getDescription(profile),
        }
    }
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 148 149 150

    showProfileSelector (): Promise<Profile|null> {
        return new Promise<Profile|null>(async (resolve, reject) => {
            try {
                const recentProfiles: Profile[] = this.config.store.recentProfiles

                let options: SelectorOption<void>[] = recentProfiles.map(p => ({
                    ...this.selectorOptionForProfile(p),
                    icon: 'fas fa-history',
                    callback: async () => {
                        if (p.id) {
                            p = (await this.getProfiles()).find(x => x.id === p.id) ?? p
                        }
                        resolve(p)
                    },
                }))
                if (recentProfiles.length) {
                    options.push({
                        name: 'Clear recent connections',
                        icon: 'fas fa-eraser',
                        callback: async () => {
                            this.config.store.recentProfiles = []
                            this.config.save()
                            resolve(null)
                        },
                    })
                }

                let profiles = await this.getProfiles()

                if (!this.config.store.terminal.showBuiltinProfiles) {
                    profiles = profiles.filter(x => !x.isBuiltin)
                }

                profiles = profiles.filter(x => !x.isTemplate)

                options = [...options, ...profiles.map((p): SelectorOption<void> => ({
                    ...this.selectorOptionForProfile(p),
                    callback: () => resolve(p),
                }))]

                try {
                    const { SettingsTabComponent } = window['nodeRequire']('tabby-settings')
                    options.push({
                        name: 'Manage profiles',
                        icon: 'fas fa-window-restore',
                        callback: () => {
                            this.app.openNewTabRaw({
                                type: SettingsTabComponent,
                                inputs: { activeTab: 'profiles' },
                            })
                            resolve(null)
                        },
                    })
                } catch { }

                if (this.getProviders().some(x => x.supportsQuickConnect)) {
                    options.push({
                        name: 'Quick connect',
                        freeInputPattern: 'Connect to "%s"...',
                        icon: 'fas fa-arrow-right',
                        callback: query => {
                            const profile = this.quickConnect(query)
                            resolve(profile)
                        },
                    })
                }
E
wording  
Eugene Pankov 已提交
151
                await this.selector.show('Select profile or enter an address', options)
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            } catch (err) {
                reject(err)
            }
        })
    }

    async quickConnect (query: string): Promise<Profile|null> {
        for (const provider of this.getProviders()) {
            if (provider.supportsQuickConnect) {
                const profile = provider.quickConnect(query)
                if (profile) {
                    return profile
                }
            }
        }
        this.notifications.error(`Could not parse "${query}"`)
        return null
    }
170 171 172 173 174 175

    getConfigProxyForProfile (profile: Profile): Profile {
        const provider = this.providerForProfile(profile)
        const defaults = configMerge(this.profileDefaults, provider?.configDefaults ?? {})
        return new ConfigProxy(profile, defaults) as unknown as Profile
    }
E
Eugene Pankov 已提交
176
}