[r62]: / trunk / cforge / cforge / Package / CFORGE / Scripts / ui.py  Maximize  Restore  History

Download this file

336 lines (288 with data), 10.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Drawing import Point, Font, FontStyle, Color, Icon
from System.Windows.Forms import Application, Form, TextBox
from System.Windows.Forms import Label, ToolBar, ToolBarButton, OpenFileDialog, Button, ScrollBars
from System.Windows.Forms import DialogResult, ScrollBars, DockStyle
import sys
import os
import json
import ntpath

GlobalResult = False

class IFormSettings(Form):

	def __init__(self, Title, Config, Folder=False, Credentials=False, Info=False, InfoText="", Message=False, PkgName=False, PkgVendor=False, PkgCopyright=False, PkgDescription=False, Repo=False):
		self.Config = Config
		
		y = 0
		self.TitleLable = Label()
		self.TitleLable.Text = Title
		self.TitleLable.Parent = self
		self.TitleLable.Height = 36
		self.TitleLable.Font = Font("Arial", 24,FontStyle.Bold)
		self.TitleLable.BackColor = Color.FromArgb(80,80,80)
		self.TitleLable.ForeColor = Color.FromArgb(255,255,255)
		self.TitleLable.Dock = DockStyle.Top
		y += 42
		
		if Repo:
			# Repository URL
			self.RepoLabel = Label()
			self.RepoLabel.Text = "Repo. URL:"
			self.RepoLabel.Location = Point(0, y)
			self.RepoLabel.Width = 60
			self.RepoLabel.Parent = self

			self.RepoBox = TextBox()
			if "Repo" in Config:
				self.RepoBox.Text = Config["Repo"]
			self.RepoBox.Enabled = True
			self.RepoBox.Location = Point(60, y)
			self.RepoBox.Parent = self
			self.RepoBox.Width = 300
			y += 24

		if Folder:
			# Folder
			self.FolderLabel = Label()
			self.FolderLabel.Text = "Folder:"
			self.FolderLabel.Location = Point(0, y)
			self.FolderLabel.Width = 60
			self.FolderLabel.Parent = self
			
			self.FolderBox = TextBox()
			if "folder" in Config:
				self.FolderBox.Text = Config["folder"]
			self.FolderBox.Enabled = True
			self.FolderBox.Location = Point(60, y)
			self.FolderBox.Parent = self
			self.FolderBox.Width = 300
			
			self.FolderButton = Button()
			self.FolderButton.Click += self.OnClickBrowse
			self.FolderButton.Text = "..."
			self.FolderButton.Parent = self
			self.FolderButton.Location = Point(364, y)
			y += 24
		
		if Credentials:
			# Credentials
			self.UserLabel = Label()
			self.UserLabel.Text = "Username:"
			self.UserLabel.Location = Point(0, y)
			self.UserLabel.Width = 60
			self.UserLabel.Parent = self

			self.UserBox = TextBox()
			if "user" in Config:
				self.UserBox.Text = Config["user"]
			self.UserBox.Enabled = True
			self.UserBox.Location = Point(60, y)
			self.UserBox.Parent = self
			self.UserBox.Width = 300
			y += 24

			self.PassLabel = Label()
			self.PassLabel.Text = "Password:"
			self.PassLabel.Location = Point(0, y)
			self.PassLabel.Width = 60
			self.PassLabel.Parent = self

			self.PassBox = TextBox()
			if "pass" in Config:
				self.PassBox.Text = Config["pass"]
			self.PassBox.Enabled = True
			self.PassBox.PasswordChar = "*"
			self.PassBox.Location = Point(60, y)
			self.PassBox.Parent = self
			self.PassBox.Width = 300
			y += 24

		if Info:
			# Info Box
			self.InfoLabel = Label()
			self.InfoLabel.Text = "Info:"
			self.InfoLabel.Location = Point(0, y)
			self.InfoLabel.Width = 60
			self.InfoLabel.Parent = self
			y += 24
			
			self.InfoBox = TextBox()
			self.InfoBox.Text = InfoText
			self.InfoBox.Enabled = True
			self.InfoBox.ReadOnly = True
			self.InfoBox.Location = Point(0, y)
			self.InfoBox.Parent = self
			self.InfoBox.WordWrap = False
			self.InfoBox.Multiline = True
			self.InfoBox.ScrollBars = ScrollBars.Horizontal | ScrollBars.Vertical
			self.InfoBox.Width = 360
			self.InfoBox.Height = 260
			y += 280
		
		if Message:
			# Message
			self.MessageLabel = Label()
			self.MessageLabel.Text = "Message:"
			self.MessageLabel.Location = Point(0, y)
			self.MessageLabel.Width = 60
			self.MessageLabel.Parent = self
			y += 24
			
			self.MessageBox = TextBox()
			self.MessageBox.Text = "Enter Message..."
			if "msg" in Config and Config["msg"] != "":
				self.MessageBox.Text = Config["msg"]
			self.MessageBox.Enabled = True
			self.MessageBox.Location = Point(0, y)
			self.MessageBox.Parent = self
			self.MessageBox.WordWrap = False
			self.MessageBox.Multiline = True
			self.MessageBox.AcceptsReturn = True
			self.MessageBox.ScrollBars = ScrollBars.Horizontal | ScrollBars.Vertical
			self.MessageBox.Width = 360
			self.MessageBox.Height = 160
			y += 180

		if PkgName:
			# Name
			self.PkgNameLabel = Label()
			self.PkgNameLabel.Text = "Name:"
			self.PkgNameLabel.Location = Point(0, y)
			self.PkgNameLabel.Width = 60
			self.PkgNameLabel.Parent = self

			self.PkgNameBox = TextBox()
			if "PkgName" in Config:
				self.PkgNameBox.Text = Config["PkgName"]
			self.PkgNameBox.Enabled = True
			self.PkgNameBox.Location = Point(60, y)
			self.PkgNameBox.Parent = self
			self.PkgNameBox.Width = 300
			y += 24
			
		if PkgVendor:
			# Name
			self.PkgVendorLabel = Label()
			self.PkgVendorLabel.Text = "Vendor:"
			self.PkgVendorLabel.Location = Point(0, y)
			self.PkgVendorLabel.Width = 60
			self.PkgVendorLabel.Parent = self

			self.PkgVendorBox = TextBox()
			self.PkgVendorBox.Text = "Open Source Software"
			if "PkgVendor" in Config and Config["PkgVendor"] != "":
				self.PkgVendorBox.Text = Config["PkgVendor"]
			self.PkgVendorBox.Enabled = True
			self.PkgVendorBox.Location = Point(60, y)
			self.PkgVendorBox.Parent = self
			self.PkgVendorBox.Width = 300
			y += 24
			
		if PkgCopyright:
			# Name
			self.PkgCopyrightLabel = Label()
			self.PkgCopyrightLabel.Text = "Copyright:"
			self.PkgCopyrightLabel.Location = Point(0, y)
			self.PkgCopyrightLabel.Width = 60
			self.PkgCopyrightLabel.Parent = self

			self.PkgCopyrightBox = TextBox()
			self.PkgCopyrightBox.Text = "all rights reserved"
			if "PkgCopyright" in Config and Config["PkgCopyright"] != "":
				self.PkgCopyrightBox.Text = Config["PkgCopyright"]
			self.PkgCopyrightBox.Enabled = True
			self.PkgCopyrightBox.Location = Point(60, y)
			self.PkgCopyrightBox.Parent = self
			self.PkgCopyrightBox.Width = 300
			y += 24
			
		if PkgDescription:
			# Name
			self.PkgDescriptionLabel = Label()
			self.PkgDescriptionLabel.Text = "Descr.:"
			self.PkgDescriptionLabel.Location = Point(0, y)
			self.PkgDescriptionLabel.Width = 60
			self.PkgDescriptionLabel.Parent = self

			self.PkgDescriptionBox = TextBox()
			if "PkgDescription" in Config:
				self.PkgDescriptionBox.Text = Config["PkgDescription"]
			self.PkgDescriptionBox.Enabled = True
			self.PkgDescriptionBox.Location = Point(60, y)
			self.PkgDescriptionBox.Parent = self
			self.PkgDescriptionBox.Width = 300
			y += 24
			
		# OK / Cancel Button
		self.AcceptButton = Button()
		self.AcceptButton.Click += self.OnClickOK
		self.AcceptButton.Text = "OK"
		self.AcceptButton.Parent = self
		self.AcceptButton.Location = Point(364, y)
		self.AcceptButton.Select()
		
		self.CancelButton = Button()
		self.CancelButton.Click += self.OnClickCancel
		self.CancelButton.Text = "Cancel"
		self.CancelButton.Parent = self
		self.CancelButton.Location = Point(0, y)
		
		y += 24
		
		# Self (Dialog)
		self.Width = 458
		self.Height = y + 50
		self.Text = "cforge - " + Title
		workingdir = os.path.dirname(sys.argv[0])
		self.Icon = Icon(os.path.join(workingdir, "icon.ico"))
		
		self.CenterToScreen()

	def OnClickBrowse(self, sender, event):
		dialog = OpenFileDialog()
		dialog.Filter = "all files (*.*) |*.*"
		dialog.ShowHelp = True
		dialog.ValidateNames = False;
		dialog.CheckFileExists = False;
		dialog.CheckPathExists = False;
		dialog.FileName = "Folder Selection"

		if dialog.ShowDialog(self) == DialogResult.OK:
			folder, file = ntpath.split(dialog.FileName)
			print (self.FolderBox)
			self.FolderBox.Text = folder

	def OnClickOK(self, sender, event):
		global GlobalResult
		# Return content of form in config structure
		if hasattr(self, 'RepoBox'):
			self.Config["repo"] = self.RepoBox.Text
		if hasattr(self, 'FolderBox'):
			self.Config["folder"] = self.FolderBox.Text
		if hasattr(self, 'UserBox'):
			self.Config["user"] = self.UserBox.Text
		if hasattr(self, 'PassBox'):
			self.Config["pass"] = self.PassBox.Text
		if hasattr(self, 'MessageBox'):
			self.Config["msg"] = self.MessageBox.Text
		if hasattr(self, 'PkgNameBox'):
			self.Config["PkgName"] = self.PkgNameBox.Text
		if hasattr(self, 'PkgVendorBox'):
			self.Config["PkgVendor"] = self.PkgVendorBox.Text
		if hasattr(self, 'PkgCopyrightBox'):
			self.Config["PkgCopyright"] = self.PkgCopyrightBox.Text
		if hasattr(self, 'PkgDescriptionBox'):
			self.Config["PkgDescription"] = self.PkgDescriptionBox.Text
		GlobalResult = True
		self.Dispose()

	def OnClickCancel(self, sender, event):
		global GlobalResult
		# Discard form data, don't return it
		GlobalResult = False
		self.Dispose()
		
	   
def GetSettings():            
	dirname = os.path.expanduser("~\\cforge")
	filename = dirname + "\\cache.json"
	config = { 'folder' : dirname, 'user' : os.environ["USERNAME"], 'pass' : '', 'msg' : ''}
	if not os.path.isfile(filename):
		if not os.path.isdir(dirname):
			os.mkdir(dirname)
		with open(filename, "w") as f:
			config = json.dump(config, f)
	with open(filename) as f:
		config = json.load(f)
		
	return config
	
def SaveSettings(config):
	dirname = os.path.expanduser("~\\cforge")
	filename = dirname + "\\cache.json"
	with open(filename, "w") as f:
		config = json.dump(config, f)
	
def Dialog(Title, Folder=False, Credentials=False, DefaultFolder="", Info=False, InfoText="", Message=False, PkgName=False, PkgVendor=False, PkgCopyright=False, PkgDescription=False, Repo=False, DefaultRepo=""):            
	config = GetSettings()
		
	if DefaultFolder != "":
		folder, file = ntpath.split(config['folder'])
		config['folder'] = os.path.join(folder, DefaultFolder)
		
	if DefaultRepo != "":
		config['Repo'] = DefaultRepo

	Application.Run(IFormSettings(Title, Folder=Folder, Credentials=Credentials, Info=Info, InfoText=InfoText, Message=Message, Config=config, PkgName=PkgName, PkgVendor=PkgVendor, PkgCopyright=PkgCopyright, PkgDescription=PkgDescription, Repo=Repo))
	if GlobalResult:
		SaveSettings(config)
		return config
	else:
		return None
	
	

#Dialog("Checkout", Folder=True, Credentials=True, DefaultFolder="test,y,z")
#Application.Run(IFormSettings("Checkout", Folder=True, Credentials=True))
#Application.Run(IFormSettings("Checkout", Credentials=True))
#Application.Run(IFormSettings("Checkout", Folder=True))