Files
azuredatastudio/extensions/liveshare/src/providers/statusProvider.ts
Anthony Dresser 7f7052ad42 Clean up some of the extensions (#8267)
* 💄

* prune unused code

* more cleanup

* remove abunch of used code
2019-11-08 11:44:43 -08:00

95 lines
3.3 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata';
import * as vscode from 'vscode';
import { LiveShare, SharedService, SharedServiceProxy } from '../liveshare';
import { ConnectionProvider } from './connectionProvider';
import { LiveShareProviderId } from '../constants';
export class LiveShareDocumentState {
public isConnected: boolean;
public serverName?: string;
public databaseName?: string;
}
export class StatusProvider {
private _sharedService: SharedService;
private _sharedServiceProxy: SharedServiceProxy;
public constructor(
private _isHost: boolean,
private _vslsApi: LiveShare,
connectionProvider: ConnectionProvider,
service: SharedService | SharedServiceProxy) {
if (this._isHost) {
this._sharedService = <SharedService>service;
this.registerStatusProvider();
} else {
this._sharedServiceProxy = <SharedServiceProxy>service;
connectionProvider.onConnect(async (args: any) => {
if (args && args.ownerUri && args.profile) {
let queryDocument = await azdata.queryeditor.getQueryDocument(args.ownerUri);
if (queryDocument) {
let connectionOptions: { [key: string]: any } = {};
connectionOptions['providerName'] = LiveShareProviderId;
connectionOptions['serverName'] = args.profile.options['server'];
connectionOptions['databaseName'] = args.profile.options['database'];
connectionOptions['userName'] = 'liveshare';
connectionOptions['password'] = 'liveshare';
connectionOptions['authenticationType'] = 'liveshare';
connectionOptions['savePassword'] = false;
connectionOptions['saveProfile'] = false;
let profile = azdata.connection.ConnectionProfile.createFrom(connectionOptions);
queryDocument.connect(profile);
}
}
});
}
}
private registerStatusProvider(): void {
let self = this;
// Retrieves the current document state associated with the URI parameter.
// The URI will be in guest Live Share format and needs to be converted back
// to the host file path format.
this._sharedService.onRequest('getDocumentState', async (args: any[]) => {
if (args && args.length > 0) {
let ownerUri = vscode.Uri.parse(args[0].ownerUri);
let localUri: vscode.Uri = self._vslsApi.convertSharedUriToLocal(ownerUri);
let connection = await azdata.connection.getConnection(localUri.toString());
let serverName: string = 'liveshare';
let databaseName: string = 'liveshare';
if (connection) {
serverName = connection.serverName;
databaseName = connection.databaseName;
}
let documentState: LiveShareDocumentState = {
isConnected: true,
serverName: serverName,
databaseName: databaseName
};
return documentState;
}
return undefined;
});
}
public getDocumentState(doc: vscode.TextDocument): Promise<LiveShareDocumentState> {
if (!this._isHost) {
return this._sharedServiceProxy.request('getDocumentState', [{
ownerUri: doc.uri.toString()
}]);
} else {
return Promise.resolve(undefined);
}
}
}