From 5ed40c7565ccc6ade11b592f6f03b070d66b2100 Mon Sep 17 00:00:00 2001 From: Jeffrey Zhang Date: Fri, 7 Aug 2015 00:53:07 +0800 Subject: [PATCH] Added list of servers in QRCode generate window to select. To add multiple servers in android phone, now it is no need to chose server one by one and generate QRCode for each. Just need to select the server listed in right. --- .../Controller/ShadowsocksController.cs | 5 ++++ .../View/QRCodeForm.Designer.cs | 18 ++++++++++++++- shadowsocks-csharp/View/QRCodeForm.cs | 23 +++++++++++++++---- shadowsocks-csharp/View/QRCodeForm.resx | 4 ++-- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index f526671e..c251400d 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -228,6 +228,11 @@ namespace Shadowsocks.Controller public string GetQRCodeForCurrentServer() { Server server = GetCurrentServer(); + return GetQRCode(server); + } + + public static string GetQRCode(Server server) + { string parts = server.method + ":" + server.password + "@" + server.server + ":" + server.server_port; string base64 = System.Convert.ToBase64String(Encoding.UTF8.GetBytes(parts)); return "ss://" + base64; diff --git a/shadowsocks-csharp/View/QRCodeForm.Designer.cs b/shadowsocks-csharp/View/QRCodeForm.Designer.cs index ca0a0006..b1cfc65d 100755 --- a/shadowsocks-csharp/View/QRCodeForm.Designer.cs +++ b/shadowsocks-csharp/View/QRCodeForm.Designer.cs @@ -29,6 +29,7 @@ private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.listBox1 = new System.Windows.Forms.ListBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // @@ -42,6 +43,19 @@ this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; // + // listBox1 + // + this.listBox1.DisplayMember = "Value"; + this.listBox1.FormattingEnabled = true; + this.listBox1.ItemHeight = 12; + this.listBox1.Location = new System.Drawing.Point(224, 10); + this.listBox1.Name = "listBox1"; + this.listBox1.ScrollAlwaysVisible = true; + this.listBox1.Size = new System.Drawing.Size(227, 208); + this.listBox1.TabIndex = 2; + this.listBox1.ValueMember = "Key"; + this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); + // // QRCodeForm // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); @@ -49,7 +63,8 @@ this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.BackColor = System.Drawing.Color.White; - this.ClientSize = new System.Drawing.Size(338, 274); + this.ClientSize = new System.Drawing.Size(457, 228); + this.Controls.Add(this.listBox1); this.Controls.Add(this.pictureBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; @@ -67,5 +82,6 @@ #endregion private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.ListBox listBox1; } } \ No newline at end of file diff --git a/shadowsocks-csharp/View/QRCodeForm.cs b/shadowsocks-csharp/View/QRCodeForm.cs index 2741d4ff..41c8ae24 100755 --- a/shadowsocks-csharp/View/QRCodeForm.cs +++ b/shadowsocks-csharp/View/QRCodeForm.cs @@ -8,8 +8,10 @@ using System.Data; using System.Drawing; using System.IO; using System.IO.Compression; +using System.Linq; using System.Text; using System.Windows.Forms; +using Shadowsocks.Model; namespace Shadowsocks.View { @@ -30,8 +32,8 @@ namespace Shadowsocks.View string qrText = ssconfig; QRCode code = ZXing.QrCode.Internal.Encoder.encode(qrText, ErrorCorrectionLevel.M); ByteMatrix m = code.Matrix; - int blockSize = Math.Max(pictureBox1.Height / m.Height, 1); - Bitmap drawArea = new Bitmap((m.Width * blockSize), (m.Height * blockSize)); + int blockSize = Math.Max(pictureBox1.Height/m.Height, 1); + Bitmap drawArea = new Bitmap((m.Width*blockSize), (m.Height*blockSize)); using (Graphics g = Graphics.FromImage(drawArea)) { g.Clear(Color.White); @@ -43,7 +45,7 @@ namespace Shadowsocks.View { if (m[row, col] != 0) { - g.FillRectangle(b, blockSize * row, blockSize * col, blockSize, blockSize); + g.FillRectangle(b, blockSize*row, blockSize*col, blockSize, blockSize); } } } @@ -54,7 +56,20 @@ namespace Shadowsocks.View private void QRCodeForm_Load(object sender, EventArgs e) { - GenQR(code); + var servers = Configuration.Load(); + var serverDatas = servers.configs.Select( + server => + new KeyValuePair(ShadowsocksController.GetQRCode(server), server.FriendlyName()) + ).ToList(); + listBox1.DataSource = serverDatas; + + var selectIndex = serverDatas.FindIndex(serverData => serverData.Key.StartsWith(code)); + if (selectIndex >= 0) listBox1.SetSelected(selectIndex, true); + } + + private void listBox1_SelectedIndexChanged(object sender, EventArgs e) + { + GenQR((sender as ListBox)?.SelectedValue.ToString()); } } } diff --git a/shadowsocks-csharp/View/QRCodeForm.resx b/shadowsocks-csharp/View/QRCodeForm.resx index 5ea0895e..29dcb1b3 100755 --- a/shadowsocks-csharp/View/QRCodeForm.resx +++ b/shadowsocks-csharp/View/QRCodeForm.resx @@ -112,9 +112,9 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 \ No newline at end of file