using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Perforce.P4;
namespace SampleApp
{
public partial class MainForm : Form
{
// Server, repository and connection.
Server server;
Repository rep;
Connection con;
// Connection details.
string uri;
string user;
string clientName;
string password;
const string LOGGEDIN = "Logged In";
const string LOGGEDOUT = "Logged Out";
const string CONNECTED = "Connected";
const string DISCONNECTED = "Disconnected";
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
SetLoggedOut();
}
private void ExitButton_Click(object sender, EventArgs e)
{
if (con.Status == ConnectionStatus.Connected) con.Disconnect();
Application.Exit();
}
private void ConnectButton_Click(object sender, EventArgs e)
{
try
{
SetLoggedOut();
ConnectToServer();
LoginToServer();
SetLoggedIn();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Application Error");
Disconnect();
}
}
private void SetLoggedIn()
{
GetButton.Enabled = true;
SubmitButton.Enabled = true;
ConnectionLabel.Text = CONNECTED;
ConnectionLabel.ForeColor = Color.Green;
LoginLabel.Text = LOGGEDIN;
LoginLabel.ForeColor = Color.Green;
Application.DoEvents();
}
private void SetLoggedOut()
{
GetButton.Enabled = false;
SubmitButton.Enabled = false;
ResultTextBox.Clear();
ConnectionLabel.Text = DISCONNECTED;
ConnectionLabel.ForeColor = Color.Red;
LoginLabel.Text = LOGGEDOUT;
LoginLabel.ForeColor = Color.Red;
Application.DoEvents();
}
public void ShowClientMap()
{
try
{
// Get client spec from repository.
Client client = rep.GetClient(clientName);
// Display spec.
ResultTextBox.Clear();
ResultTextBox.Text = client.ViewMap.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Application Error");
Disconnect();
}
}
public void LoginToServer()
{
try
{
user = UserTextBox.Text.Trim();
clientName = ClientTextBox.Text.Trim();
// Set username and client name.
con.UserName = user;
con.Client = new Client();
con.Client.Name = clientName;
password = PasswordTextBox.Text.Trim();
Credential cred = con.Login(password, null, null);
LoginLabel.Text = LOGGEDIN;
LoginLabel.ForeColor = Color.Green;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Application Error");
Disconnect();
}
}
public void ConnectToServer()
{
// Connection details.
//uri = "public.perforce.com:1666";
//uri = @"rsh:c:\test\p4d.exe -r C:\test -L log -i";
//user = "Paul_Arva";
//clientName = "Paul_Arva_SampleApp";
//password = "Perpass1";
ResultTextBox.Clear();
uri = ServerTextBox.Text.Trim();
server = new Server(new ServerAddress(uri));
rep = new Repository(server);
con = rep.Connection;
try
{
// Connect to server.
con.Connect(null);
// Successful connection.
ConnectionLabel.Text = CONNECTED;
ConnectionLabel.ForeColor = Color.Green;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Application Error");
Disconnect();
}
}
private void SubmitButton_Click(object sender, EventArgs e)
{
try
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Application Error");
Disconnect();
}
}
private void GetButton_Click(object sender, EventArgs e)
{
try
{
ShowClientMap();
SubmitButton.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Application Error");
Disconnect();
}
}
private void Disconnect()
{
if (con.Status == ConnectionStatus.Connected) con.Disconnect();
ConnectButton.Enabled = true;
GetButton.Enabled = false;
SubmitButton.Enabled = false;
}
}
}