Fixed Syncthing folder status events and exec command.
[src/xds/xds-agent.git] / webapp / src / app / services / config.service.ts
1 import { Injectable } from '@angular/core';
2 import { CookieService } from 'ngx-cookie';
3 import { Observable } from 'rxjs/Observable';
4 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
5
6 import { AlertService, IAlert } from "../services/alert.service";
7 import { UtilsService } from "../services/utils.service";
8
9 export interface IConfig {
10     projectsRootDir: string;
11 }
12
13 @Injectable()
14 export class ConfigService {
15
16     public Conf$: Observable<IConfig>;
17
18     private confSubject: BehaviorSubject<IConfig>;
19     private confStore: IConfig;
20
21     constructor(private _window: Window,
22         private cookie: CookieService,
23         private alert: AlertService,
24         private utils: UtilsService,
25     ) {
26         this.load();
27         this.confSubject = <BehaviorSubject<IConfig>>new BehaviorSubject(this.confStore);
28         this.Conf$ = this.confSubject.asObservable();
29     }
30
31     // Load config
32     load() {
33         // Try to retrieve previous config from cookie
34         let cookConf = this.cookie.getObject("xds-config");
35         if (cookConf != null) {
36             this.confStore = <IConfig>cookConf;
37         } else {
38             // Set default config
39             this.confStore = {
40                 projectsRootDir: "",
41                 //projects: []
42             };
43         }
44     }
45
46     // Save config into cookie
47     save() {
48         // Notify subscribers
49         this.confSubject.next(Object.assign({}, this.confStore));
50
51         // Don't save projects in cookies (too big!)
52         let cfg = Object.assign({}, this.confStore);
53         this.cookie.putObject("xds-config", cfg);
54     }
55
56     set projectsRootDir(p: string) {
57         this.confStore.projectsRootDir = p;
58         this.save();
59     }
60 }