From: minkee.lee Date: Wed, 24 Dec 2014 12:26:12 +0000 (+0900) Subject: Network: Added network config option. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=94588be56d7a81fb1cf6a061e1222e250ed92c96;p=sdk%2Femulator%2Femulator-manager.git Network: Added network config option. - Tun/tap, proxy. Change-Id: Ic71960d2aca261d958d1986250aadf5ca9849241 Signed-off-by: minkee.lee --- diff --git a/build.xml b/build.xml index 9131cbf..bf64bc5 100644 --- a/build.xml +++ b/build.xml @@ -8,6 +8,7 @@ + @@ -18,6 +19,7 @@ + @@ -52,7 +54,7 @@ - + @@ -82,6 +84,7 @@ + diff --git a/common-project/src/org/tizen/emulator/manager/tool/TapUtil.java b/common-project/src/org/tizen/emulator/manager/tool/TapUtil.java new file mode 100644 index 0000000..c2222dc --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/tool/TapUtil.java @@ -0,0 +1,923 @@ +/* Emulator Manager + * + * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * SeokYeon Hwang + * Sangho Park + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.tool; + +import java.io.IOException; +import java.net.Inet4Address; +import java.net.InterfaceAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.List; +import java.util.regex.Pattern; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.tizen.emulator.manager.EmulatorManager; +import org.tizen.emulator.manager.logging.EMLogger; +import org.tizen.emulator.manager.ui.detail.item.template.ComboViewItem; +import org.tizen.emulator.manager.ui.dialog.MessageDialog; +import org.tizen.emulator.manager.ui.dialog.TapGuideDialogForWin; +import org.tizen.emulator.manager.vms.helper.HelperClass; +import org.tizen.emulator.manager.vms.helper.ProcessOutputReader; +import org.tizen.emulator.manager.vms.helper.ProcessResult; +import org.tizen.emulator.manager.vms.helper.VMWorkerException; + +import com.sun.jna.platform.win32.Advapi32Util; +import com.sun.jna.platform.win32.Win32Exception; +import com.sun.jna.platform.win32.WinNT; +import com.sun.jna.platform.win32.WinReg; +import com.sun.jna.platform.win32.WinReg.HKEY; +import com.sun.jna.platform.win32.WinReg.HKEYByReference; + +public class TapUtil { + + public static String getBridgeFromDevice(String device) + throws VMWorkerException { + + String result = null; + if (EmulatorManager.isLinux()) { + List cmd = Arrays.asList("brctl", "show"); + ProcessResult res = HelperClass.runProcess(cmd); + + if (res.isSuccess() == false) { + throw new VMWorkerException(res.getResultMessage()); + } + + String parsedBridge = null; + String parsedDevice = null; + for (String line : res.getStdOutMsg()) { + String[] arr = line.split("\\s+"); + if (arr.length == 4) { + parsedBridge = arr[0].trim(); + parsedDevice = arr[3].trim(); + + } else if (arr.length == 2 && arr[0].isEmpty()) { + parsedDevice = arr[1].trim(); + + } else { + continue; + } + if (device.equals(parsedDevice)) { + result = parsedBridge; + } + + } + } + + return result; + } + + public static List getDefaultGateway(String ifName) + throws VMWorkerException { + List result = new ArrayList(); + + if (EmulatorManager.isLinux()) { + // If ifName == null, return all default gw. + List cmd = Arrays.asList("ip", "route"); + ProcessResult res = HelperClass.runProcess(cmd); + if (res.isSuccess() == true) { + for (String str : res.getStdOutMsg()) { + String[] arr = str.split("\\s+"); + if (ifName == null) { + if (arr.length >= 3) { + if (arr[0].equals("default")) { + result.add(arr[2]); + } + } + + } else { + if (arr.length >= 5) { + if (arr[0].equals("default") + && arr[4].equals(ifName)) { + result.add(arr[2]); + } + } + } + } + + } else { + throw new VMWorkerException(res.getResultMessage()); + } + } + return result; + } + + public static List getBridgeList() throws VMWorkerException { + List result = new ArrayList(); + if (EmulatorManager.isLinux()) { + List cmd = Arrays.asList("brctl", "show"); + ProcessResult res = HelperClass.runProcess(cmd); + if (res.isSuccess() == true) { + for (String str : res.getStdOutMsg()) { + String[] arr = str.split("\\s+"); + if (arr.length > 4) + continue; // Skip header line + if (!arr[0].isEmpty()) { + result.add(arr[0]); + } + } + + } else { + throw new VMWorkerException(res.getResultMessage()); + } + } + return result; + + } + + public static List getTapList() { + List list = new ArrayList(); + + // Get tap device list from Host OS. + if (EmulatorManager.isLinux()) { + for (String tap : getTapListForLinux()) { + try { + // Except not-bridged tap. + if (getBridgeFromDevice(tap) != null) { + list.add(tap); + } + } catch (VMWorkerException e) { + EMLogger.getLogger().warning(e.getMessage()); + } + } + + } else if (EmulatorManager.isWin()) { + list = getTapListForWin2(); + + } else if (EmulatorManager.isMac()) { + // TODO + } + return list; + } + + // Use "ip tuntap" command. + // Result is like below. + // "tap1: tap one_queue vnet_hdr" + // We need "tap1" in result. + private static List getTapListForLinux() { + List result = new ArrayList(); + List cmd = Arrays.asList("ip", "tuntap"); + ProcessResult res = HelperClass.runProcess(cmd); + if (res.isSuccess() == false) { + EMLogger.getLogger().warning( + "Get tap list fail. Command returns fail:" + "cmd : " + + cmd.toString() + ", return : " + + res.getExitValue()); + } else { + for (String line : res.getStdOutMsg()) { + String arr[] = line.split(":"); + if (arr.length > 0) { + result.add(arr[0].trim()); + } + } + } + return result; + } + + // Use "openvpn --show-adapters" command. + // Result is like below. + // "'Local Area Connection 2' {9BDB05CA-...}" + // We need "Local Area Connection 2" in result. + @Deprecated + private static List getTapListForWin() { + List result = new ArrayList(); + List cmd = new ArrayList(); + ProcessBuilder pb = new ProcessBuilder(new String[] { "openvpn", + "--show-adapters" }); + int exitValue = 0; + Process process; + try { + process = pb.start(); + List stdOut = ProcessOutputReader.readStdOut(process, + cmd.toString()); + exitValue = process.waitFor(); + + if (exitValue != 0) { + EMLogger.getLogger().warning( + "Get tap list fail. Command returns fail:" + "cmd : " + + cmd.toString() + ", return : " + exitValue); + } else { + for (String line : stdOut) { + if (line.contains("'")) { + String arr[] = line.split("'"); + if (arr.length >= 2) { + result.add(arr[1].trim()); + } + } + } + } + + } catch (IOException e) { + EMLogger.getLogger() + .warning("Get tap list fail. " + e.getMessage()); + e.printStackTrace(); + + } catch (InterruptedException e) { + EMLogger.getLogger() + .warning("Get tap list fail. " + e.getMessage()); + e.printStackTrace(); + } + + return result; + } + + private static List getTapListForWin2() { + List tapNameList = new ArrayList(); + List tapIdList = getTapIdList(); + HKEY root = WinReg.HKEY_LOCAL_MACHINE; + String topKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"; + String key = "Connection"; + String value = "Name"; + HKEYByReference regKey = null; + try { + regKey = Advapi32Util.registryGetKey(root, topKey, WinNT.KEY_READ); + String[] subKeys = Advapi32Util.registryGetKeys(regKey.getValue()); + for (String subKey : subKeys) { + for (String tapId : tapIdList) { + if (subKey.equals(tapId)) { + String[] subKeys2 = Advapi32Util.registryGetKeys(root, topKey + "\\" + subKey); + for (String subKey2 : subKeys2) { + if (subKey2.equals(key)) { + String tapName = Advapi32Util.registryGetStringValue(root, topKey + "\\" + subKey + "\\" + key, value); + if (tapName != null) { + tapNameList.add(tapName); + } + } + } + } + } + } + + } catch (Win32Exception e) { + + } finally { + if (regKey != null) { + Advapi32Util.registryCloseKey(regKey.getValue()); + } + } + + return tapNameList; + } + + private static List getTapIdList() { + + List tapIdList = new ArrayList(); + HKEY root = WinReg.HKEY_LOCAL_MACHINE; + String topKey= "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"; + + String value1 = "ComponentId"; + String value2 = "NetCfgInstanceId"; + HKEYByReference regKey = null; + try { + regKey = Advapi32Util.registryGetKey(root, topKey, WinNT.KEY_READ); + String[] subKeys = Advapi32Util.registryGetKeys(regKey.getValue()); + for (String subKey : subKeys) { + String compId = Advapi32Util.registryGetStringValue(root, topKey + "\\" + subKey, value1); + String tapId = Advapi32Util.registryGetStringValue(root, topKey + "\\" + subKey, value2); + if (compId != null && tapId != null) { + if (compId.startsWith("tap")) { + tapIdList.add(tapId); + } + } + } + + } catch (Win32Exception e) { + + } finally { + if (regKey != null) { + Advapi32Util.registryCloseKey(regKey.getValue()); + } + } + + return tapIdList; + } + + public static boolean isTapInBridge(String tapName) { + boolean result = false; + if (EmulatorManager.isWin()) { + String foundTapId = null; + List tapIdList = getTapIdList(); + HKEY root = WinReg.HKEY_LOCAL_MACHINE; + String topKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"; + String key = "Connection"; + String value = "Name"; + HKEYByReference regKey = null; + try { + regKey = Advapi32Util.registryGetKey(root, topKey, WinNT.KEY_READ); + String[] subKeys = Advapi32Util.registryGetKeys(regKey.getValue()); + for (String subKey : subKeys) { + for (String tapId : tapIdList) { + if (subKey.equals(tapId)) { + String[] subKeys2 = Advapi32Util.registryGetKeys(root, topKey + "\\" + subKey); + for (String subKey2 : subKeys2) { + if (subKey2.equals(key)) { + String name = Advapi32Util.registryGetStringValue(root, topKey + "\\" + subKey + "\\" + key, value); + if (name != null) { + if (name.equals(tapName)) { + foundTapId = tapId; + } + } + } + } + } + } + } + + topKey = "SYSTEM\\CurrentControlSet\\services\\Bridge\\Linkage"; + value = "Route"; + String valueArr[] = Advapi32Util.registryGetStringArray(root, topKey, value); + for (String str : valueArr) { + if (str.replace("\"", "").equals(foundTapId)) { + result = true; + break; + } + } + + } catch (Win32Exception e) { + + } finally { + if (regKey != null) { + Advapi32Util.registryCloseKey(regKey.getValue()); + } + } + + } else if (EmulatorManager.isLinux()) + try { + if (getBridgeFromDevice(tapName) != null) + result = true; + } catch (VMWorkerException e) { + (new MessageDialog()).openWarningDialog(e.getMessage()); + } + return result; + } + + public static String getNetmaskFromTap(String tapName) { + String netMask = ""; + if (EmulatorManager.isLinux()) + try { + String bridge = getBridgeFromDevice(tapName); + InterfaceAddress addr = getInterfaceIpInfo(bridge); + if (addr != null) + netMask = getNetMask(addr.getNetworkPrefixLength()); + } catch (VMWorkerException e) { + EMLogger.getLogger().warning(e.getMessage()); + } + else if (EmulatorManager.isWin() && isTapInBridge(tapName)) { + HKEY key = getBridgeTcpipKey(); + if (key != null) { + try { + String data[] = Advapi32Util.registryGetStringArray(key, "SubnetMask"); + if (data.length > 0) { + netMask = data[0]; + } + + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + Advapi32Util.registryCloseKey(key); + } + } + + if (netMask.isEmpty()) { + key = getInterfaceTcpipKey(getBridgeId()); + if (key != null) { + try { + String data[] = Advapi32Util.registryGetStringArray(key, "SubnetMask"); + if (data.length > 0) { + netMask = data[0]; + } + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + Advapi32Util.registryCloseKey(key); + } + } + } + } + return netMask; + } + + private static HKEY getBridgeTcpipKey() { + HKEY resultKey = null; + String bridgeId = getBridgeId(); + if (bridgeId == null) { + return resultKey; + } + + HKEY root = WinReg.HKEY_LOCAL_MACHINE; + String topKey = "SYSTEM\\CurrentControlSet\\services"; + HKEYByReference regKey = null; + try { + regKey = Advapi32Util.registryGetKey(root, topKey, WinNT.KEY_READ); + String[] subKeys = Advapi32Util.registryGetKeys(regKey.getValue()); + for (String subKey : subKeys) { + if (subKey.equals(bridgeId)) { + resultKey = Advapi32Util.registryGetKey(root, + topKey + "\\" + subKey + "\\Parameters\\Tcpip", WinNT.KEY_READ).getValue(); + break; + } + } + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + if (regKey != null) { + Advapi32Util.registryCloseKey(regKey.getValue()); + } + } + return resultKey; + } + + private static HKEY getInterfaceTcpipKey(String ifId) { + if (ifId == null) { + return null; + } + HKEY resultKey = null; + + HKEY root = WinReg.HKEY_LOCAL_MACHINE; + String topKey = "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces" + + "\\" + ifId; + + try { + resultKey = Advapi32Util.registryGetKey(root, topKey, WinNT.KEY_READ).getValue(); + + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } + return resultKey; + } + + private static String getBridgeId() { + String bridgeId = null; + if (EmulatorManager.isWin()) { + HKEY root = WinReg.HKEY_LOCAL_MACHINE; + String topKey = "SYSTEM\\CurrentControlSet\\services\\BridgeMP"; + String value = "Device"; + try { + String data = Advapi32Util.registryGetStringValue(root, topKey, value); + if (data != null) { + String arr[] = data.split("\\\\"); + bridgeId = arr[arr.length - 1]; + } + } catch (Win32Exception e) { + + } + } + return bridgeId; + } + + public static String getGatewayFromTap(String tapName) { + String gateway = ""; + if (EmulatorManager.isLinux()) + try { + List gwList = getDefaultGateway(getBridgeFromDevice(tapName)); + if (gwList.size() == 1) + gateway = (String) gwList.get(0); + } catch (VMWorkerException e) { + EMLogger.getLogger().warning(e.getMessage()); + } + else if (EmulatorManager.isWin() && isTapInBridge(tapName)) { + HKEY key = getBridgeTcpipKey(); + if (key != null) { + try { + String data[] = Advapi32Util.registryGetStringArray(key, "DefaultGateway"); + if (data.length > 0) { + gateway = data[0]; + } + + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + Advapi32Util.registryCloseKey(key); + } + } + + if (gateway.isEmpty()) { + key = getInterfaceTcpipKey(getBridgeId()); + if (key != null) { + try { + String data[] = Advapi32Util.registryGetStringArray(key, "DefaultGateway"); + if (data.length > 0) { + gateway = data[0]; + } + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + Advapi32Util.registryCloseKey(key); + } + } + } + } + return gateway; + } + + public static String getDnsFromTap(String tapName) { + String dns=""; + if (EmulatorManager.isLinux()) { + dns = getDnsForLinux(tapName); + + } else if (EmulatorManager.isWin() && isTapInBridge(tapName)) { + dns = getDnsForWin(tapName); + } + return dns; + } + + private static String getDnsForLinux(String tapName) { + String brName = null; + try { + brName = getBridgeFromDevice(tapName); + + } catch (VMWorkerException e) { + EMLogger.getLogger().warning("Get dns failed. " + e.getMessage()); + return ""; + } + + if (brName == null) { + return ""; + } + + List cmd = Arrays.asList("nm-tool", brName); + ProcessResult res = HelperClass.runProcess(cmd); + if (!res.isSuccess()) { + EMLogger.getLogger().warning("Get dns failed. " + res.getResultMessage()); + return ""; + } + + for (String line : res.getStdOutMsg()) { + String[] arr = line.split("\\s+"); + if (arr.length >= 3) { + if (arr[1].trim().startsWith("DNS")) { + return arr[2].trim(); + } + } + } + + return ""; + } + + private static String getDnsForWin(String tapName) { + String dns = ""; + HKEY key = getInterfaceTcpipKey(getBridgeId()); + if (key != null) { + try { + String val = Advapi32Util.registryGetStringValue(key, "NameServer"); + String[] arr = val.split(","); + if (arr.length > 0) { + dns = arr[0]; + } + + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + Advapi32Util.registryCloseKey(key); + } + } + + return dns; + } + + + // return ip, broadcast, mask-length + public static InterfaceAddress getInterfaceIpInfo(String ifName) { + Enumeration interfaces; + try { + interfaces = NetworkInterface.getNetworkInterfaces(); + if (interfaces != null) { + while (interfaces.hasMoreElements()) { + NetworkInterface current = interfaces.nextElement(); + if (current.getName().equals(ifName)) { + for (InterfaceAddress addr : current + .getInterfaceAddresses()) { + if (addr.getAddress() instanceof Inet4Address) { + return addr; + } + } + } + } + } + } catch (SocketException e) { + EMLogger.getLogger().warning(e.getMessage()); + } + return null; + } + + public static String getNetMask(int maskLength) { + StringBuilder netMaskAddr = new StringBuilder(); + int val = maskLength / 8; + int mod = maskLength % 8; + int count = 0; + + for (int i = 0; i < val; i++) { + if (i > 0) { + netMaskAddr.append("."); + } + netMaskAddr.append("255"); + count++; + } + + if (mod != 0) { + int modMask = 0; + for (int i = 7; i > 7 - mod; i--) { + modMask += Math.pow(2, i); + } + netMaskAddr.append(".").append(String.valueOf(modMask)); + count++; + } + + for (; count < 4; count++) { + netMaskAddr.append(".0"); + } + + return netMaskAddr.toString(); + } + + static private final String IPV4_REGEX = "(([0-1]?[0-9]{1,2}\\.)|(2[0-4][0-9]\\.)|(25[0-5]\\.)){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))"; + static private Pattern IPV4_PATTERN = Pattern.compile(IPV4_REGEX); + + public static boolean checkIPString(boolean widgetEnable, String ip, + String title) { + boolean result = true; + String errMsg = ""; + if (widgetEnable == true) { + if (ip == null || ip.isEmpty()) { + errMsg = title + " cannot be empty!"; + result = false; + + } else { + result = IPV4_PATTERN.matcher(ip).matches(); + if (result == false) { + errMsg = title + " is invalid!"; + } + } + } + + HelperClass.setStatusBar(errMsg); + return result; + } + + public static boolean checkIP(String ip) { + return IPV4_PATTERN.matcher(ip).matches(); + } + + public static boolean nameNotExist(String tapName) { + boolean isNotExist = true; + List tapList = null; + + if (EmulatorManager.isWin()) { + tapList = getTapListForWin2(); + + } else if (EmulatorManager.isLinux()) { + tapList = getTapListForLinux(); + } + + if (tapList != null) { + for (String str : tapList) { + if (str.equals(tapName)) { + isNotExist = false; + break; + } + } + } + return isNotExist; + } + + public static void createTapDevice(String tapName, + ComboViewItem comboViewItem) { + if (EmulatorManager.isWin()) { + MessageDialog msgDialog = new MessageDialog(); + msgDialog + .openNoButtonInfoDialog("Creating tap device.\nThis will take few seconds.."); + TapCreateWorker worker = new TapCreateWorker(tapName, msgDialog, + comboViewItem); + worker.start(); + } + + } + + public static String getBridgeIpAddr(String tapName) { + String ipAddr = null; + if (EmulatorManager.isLinux()) + try { + InterfaceAddress ifAddr = getInterfaceIpInfo(getBridgeFromDevice(tapName)); + if (ifAddr != null) + ipAddr = ifAddr.getAddress().getHostAddress(); + } catch (VMWorkerException e) { + EMLogger.getLogger().warning( + (new StringBuilder()) + .append("Failed to get bridge ip address. ") + .append(e.getMessage()).toString()); + } + else if (EmulatorManager.isWin() && isTapInBridge(tapName)) { + HKEY key = getBridgeTcpipKey(); + if (key != null) { + try { + String data[] = Advapi32Util.registryGetStringArray(key, "IPAddress"); + if (data.length > 0) { + ipAddr = data[0]; + } + + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + Advapi32Util.registryCloseKey(key); + } + } + + if (ipAddr == null) { + key = getInterfaceTcpipKey(getBridgeId()); + if (key != null) { + try { + String data[] = Advapi32Util.registryGetStringArray(key, "IPAddress"); + if (data.length > 0) { + ipAddr = data[0]; + } + } catch (Win32Exception e) { + EMLogger.getLogger().warning(e.getMessage()); + } finally { + Advapi32Util.registryCloseKey(key); + } + } + } + } + return ipAddr; + } + + public static boolean isValidTap(String tapName) throws VMWorkerException { + if (getGatewayFromTap(tapName).isEmpty()) { + return false; + } + if (getNetmaskFromTap(tapName).isEmpty()) { + return false; + } + return getBridgeIpAddr(tapName) != null; + } + + public static String getAvailableTapName() { + List list = null; + if (EmulatorManager.isLinux()) { + list = getTapListForLinux(); + + } else if (EmulatorManager.isWin()) { + list = getTapListForWin2(); + } + + String newName = ""; + if (list != null) { + for (int i = 1;; i++) { + newName = "tap" + String.valueOf(i); + boolean nameIsSame = false; + for (String tap : list) { + if (newName.equals(tap)) { + nameIsSame = true; + break; + } + } + if (!nameIsSame) { + break; + } + } + } + + return newName; + } +} + +class TapCreateWorker extends Thread { + + final MessageDialog dialog; + final String tapName; + final ComboViewItem comboViewItem; // for refresh combo-list + + public TapCreateWorker(String tapName, MessageDialog dialog, + ComboViewItem comboViewItem) { + this.dialog = dialog; + this.tapName = tapName; + this.comboViewItem = comboViewItem; + } + + @Override + public void run() { + // Create tap device + // - Create tap device and rename(user input name) because + // windows OS generate device name automatically. + // - For rename, inquire tap list before and after create and + // compare both to find what is new one. + try { + // 1. Get tap list before create new tap. + List before = TapUtil.getTapList(); + try { // TODO for test + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // 2. Create tap + List cmd = Arrays.asList("tapinstall", "install", + "OemVista.inf", "Tap0901"); + ProcessResult res = HelperClass.runProcess(cmd); + + if (!res.isSuccess()) { + throw new VMWorkerException(res.getResultMessage()); + } + + // 3. Get tap list and find new one. + List after = TapUtil.getTapList(); + String newOne = null; + for (String str : after) { + if (!before.contains(str)) { + newOne = str; + break; + } + } + + if (newOne == null) { + throw new VMWorkerException( + "Tap create fail. Cannot find new tap device."); + } + + // 4. Rename + cmd = Arrays.asList("netsh", "interface", "set", "interface", + "name=" + newOne, "newname=" + tapName); + res = HelperClass.runProcess(cmd); + if (!res.isSuccess()) { + throw new VMWorkerException(res.getResultMessage()); + } + + // 5. Check tap create in success. + boolean isTapExist = false; + for (String str : TapUtil.getTapList()) { + if (str.equals(tapName)) { + isTapExist = true; + } + } + + final MessageDialog resultDialog = new MessageDialog(); + if (isTapExist) { + Display.getDefault().asyncExec(new Runnable() { + public void run() { + int res = resultDialog + .openSelectionDialog("Tap device created : " + + tapName + + "\nDo you want to see the guide for bridge network ?"); + if (res == SWT.OK) { + // Show bridge guide dialog + TapGuideDialogForWin.open(); + } + } + }); + + } else { + throw new VMWorkerException("Tap is not exist : " + tapName); + } + + } catch (VMWorkerException e) { + EMLogger.getLogger().warning("Failed to create tap device."); + EMLogger.getLogger().warning(e.getMessage()); + final String msg = e.getMessage(); + Display.getDefault().asyncExec(new Runnable() { + public void run() { + new MessageDialog() + .openWarningDialog("\nFailed to create tap device.\n\n" + + msg); + } + }); + + } finally { + Display.getDefault().asyncExec(new Runnable() { + public void run() { + dialog.close(); + comboViewItem.resetCombo(); + } + }); + } + + } +} diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/CommonItemListFactory.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/CommonItemListFactory.java index 905449d..1c5dab9 100644 --- a/common-project/src/org/tizen/emulator/manager/ui/detail/item/CommonItemListFactory.java +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/CommonItemListFactory.java @@ -68,6 +68,13 @@ public abstract class CommonItemListFactory implements IItemListFactory{ public static String ITEM_FILE_SHARE = "fileShare"; public static String ITEM_HW_SUPPORT = "hwSupport"; public static String ITEM_PROCESSORS = "processors"; + public static String ITEM_NET_CONFIG = "netConfig"; + public static String ITEM_NET_PROXY = "proxy"; + public static String ITEM_NET_CONNECT_TYPE = "netConnectType"; + public static String ITEM_NET_TAP_DEVICE = "netTapDevice"; + public static String ITEM_NET_IP_INFO = "netIpInfo"; + public static String ITEM_NET_DNS = "netDns"; + public static String ITEM_NET_MAC = "netMac"; public static ObjectFactory factory = new ObjectFactory(); diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetConnectTypeViewItem.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetConnectTypeViewItem.java new file mode 100644 index 0000000..5d7b9c7 --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetConnectTypeViewItem.java @@ -0,0 +1,142 @@ +/* + * Emulator Manager + * + * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * JiHye Kim + * SeokYeon Hwang + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.ui.detail.item.property; + +import org.eclipse.swt.events.SelectionEvent; + +import org.eclipse.swt.events.SelectionListener; +import org.tizen.emulator.manager.ui.detail.item.AdvancedViewItem; +import org.tizen.emulator.manager.ui.detail.item.CommonItemListFactory; +import org.tizen.emulator.manager.ui.detail.item.LineLabelViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.ComboViewItem; +import org.tizen.emulator.manager.vms.VMPropertyValue; +import org.tizen.emulator.manager.vms.xml.template.Item; + +public class NetConnectTypeViewItem extends ComboViewItem { + + public static final String VALUE_NAT = "NAT"; + public static final String VALUE_BRIDGE = "Bridge"; + + public NetConnectTypeViewItem(Item template, + LineLabelViewItem lineLabelViewItem) { + super(template, lineLabelViewItem); + lineLabelViewItem.addItem(name, this); + } + + @Override + protected void addWidgetListener() { + combo.addSelectionListener(new SelectionListener() { + @Override + public void widgetSelected(SelectionEvent e) { + newValue = combo.getText(); + + // Disable / enable following item - bridge name, ip info, dns + AdvancedViewItem item = lineLabelViewItem + .getItem(CommonItemListFactory.ITEM_NET_IP_INFO); + if (item != null && item instanceof NetIPInfoViewItem) { + ((NetIPInfoViewItem) item).resetSubItems(newValue); + } + + item = lineLabelViewItem + .getItem(CommonItemListFactory.ITEM_NET_TAP_DEVICE); + if (item != null && item instanceof NetTapDeviceViewItem) { + ((NetTapDeviceViewItem) item).resetCombo(newValue); + } + + item = lineLabelViewItem.getItem(CommonItemListFactory.ITEM_NET_DNS); + if (item != null && item instanceof NetDnsViewItem) { + ((NetDnsViewItem) item).resetText(newValue); + } + + item = lineLabelViewItem.getItem(CommonItemListFactory.ITEM_NET_MAC); + if (item != null && item instanceof NetMacViewItem) { + ((NetMacViewItem) item).resetText(newValue); + } + + if (isCreateMode()) { + getListener().changeConfirmButton(); + + } else { + getListener().changeConfirmButton(getThis()); + } + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + // TODO Auto-generated method stub + } + }); + } + + public String getValue() { + return newValue; + } + + @Override + public boolean settingModifyItem(VMPropertyValue value) { + + if (isCreateMode()) { + newValue = VALUE_NAT; // TODO use template option + // (defaultOnCreate).. + } else { + newValue = getItemValue(value); + } + + // makeComboBox + if (comboOptions != null) { + if (combo != null) { + combo.removeAll(); + } + for (String option : comboOptions) { + combo.add(option); + } + } + + // Select combo-box item. + int index = -1; + for (int i = 0; i < comboOptions.size(); i++) { + if (comboOptions.get(i).equals(newValue)) { + index = i; + break; + } + } + if (comboOptions.size() > 0) { + if (index == -1) { + // if value not matches.. + combo.select(0); + newValue = combo.getItem(0); + } else { + combo.select(index); + } + } + return true; + } +} diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetDnsViewItem.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetDnsViewItem.java new file mode 100644 index 0000000..1aa79dd --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetDnsViewItem.java @@ -0,0 +1,145 @@ +/* + * Emulator Manager + * + * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * JiHye Kim + * SeokYeon Hwang + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.ui.detail.item.property; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.tizen.emulator.manager.tool.TapUtil; +import org.tizen.emulator.manager.ui.detail.item.AdvancedViewItem; +import org.tizen.emulator.manager.ui.detail.item.CommonItemListFactory; +import org.tizen.emulator.manager.ui.detail.item.ItemChangeState; +import org.tizen.emulator.manager.ui.detail.item.LineLabelViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.TextViewItem; +import org.tizen.emulator.manager.vms.VMPropertyValue; +import org.tizen.emulator.manager.vms.xml.template.Item; + +public class NetDnsViewItem extends TextViewItem { + + public NetDnsViewItem(Item template, LineLabelViewItem lineLabelViewItem) { + super(template, lineLabelViewItem); + lineLabelViewItem.addItem(name, this); + } + + public void setDns(String dns) { + newValue = oldValue = dns; + text.setText(dns); + } + + public String getDns() { + return newValue; + } + + public void resetText(String connectType) { + if (connectType.equals(NetConnectTypeViewItem.VALUE_NAT)) { + // disable text input + text.setEnabled(false); + oldValue = newValue; // save + newValue = ""; + + } else if (connectType.equals(NetConnectTypeViewItem.VALUE_BRIDGE)) { + // enable text input + text.setEnabled(true); + newValue = oldValue; // restore + } + + text.setText(newValue); + } + + @Override + public boolean settingModifyItem(VMPropertyValue value) { + + if (isCreateMode()) { + oldValue = newValue = ""; + } else { + oldValue = newValue = value.getAdvancedOptionValue(name); + } + text.setText(newValue); + + AdvancedViewItem item = lineLabelViewItem + .getItem(CommonItemListFactory.ITEM_NET_CONNECT_TYPE); + if (item != null && item instanceof NetConnectTypeViewItem) { + String connectType = ((NetConnectTypeViewItem) item).getValue(); + resetText(connectType); + } + + // If use DHCP, set disabled. + item = lineLabelViewItem.getItem(CommonItemListFactory.ITEM_NET_IP_INFO); + if (item != null && item instanceof NetIPInfoViewItem) { + if (((NetIPInfoViewItem)item).useDHCP()) { + setEnabled(false); + } + } + return false; + } + + @Override + public void addListener() { + text.addModifyListener(new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + newValue = text.getText(); + + if (isCreateMode()) { + getListener().changeConfirmButton(); + } else { + getListener().changeConfirmButton(getThis()); + } + } + }); + + text.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent event) { + switch (event.keyCode) { + case SWT.CR: + case SWT.KEYPAD_CR: + // TODO + getListener().ChangeState(ItemChangeState.CREATE); + break; + case SWT.ESC: + getListener().ChangeState(ItemChangeState.CANCEL); + break; + default: + // TODO + break; + } + } + }); + } + + @Override + public boolean checkValue() { + return TapUtil.checkIPString(text.isEnabled(), newValue, title); + } + +} diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoDHCPSubViewItem.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoDHCPSubViewItem.java new file mode 100644 index 0000000..d395b87 --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoDHCPSubViewItem.java @@ -0,0 +1,145 @@ +/* + * Emulator Manager + * + * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * SeokYeon Hwang + * sangho Park + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.ui.detail.item.property; + +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.widgets.Composite; +import org.tizen.emulator.manager.ui.detail.item.AdvancedViewItem; +import org.tizen.emulator.manager.ui.detail.item.CommonItemListFactory; +import org.tizen.emulator.manager.ui.detail.item.template.CheckSubViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.ISubViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.LabelViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.TextSubViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.TextViewItem; +import org.tizen.emulator.manager.vms.VMPropertyValue; +import org.tizen.emulator.manager.vms.xml.template.Item; + +public class NetIPInfoDHCPSubViewItem extends CheckSubViewItem { + + public NetIPInfoDHCPSubViewItem(LabelViewItem parentItem, Composite comp, + Item template, boolean isOdd) { + super(parentItem, comp, template, isOdd); + defaultIfEmpty = false; + } + + public void resetCheckbox(String connectType) { + if (modifyEnable == true) { // config from template. + if (connectType.equals(NetConnectTypeViewItem.VALUE_NAT)) { + setCheckBox(false); + setEnabled(false); + + } else if (connectType.equals(NetConnectTypeViewItem.VALUE_BRIDGE)) { +// setCheckBox(newValue ? true : false); + setEnabled(true); + } + } + } + + @Override + public boolean settingModifyItem(VMPropertyValue value) { + + // Set value. + String val = getModifyValue(value); + newValue = oldValue = checkOnOff(val); + + // Set enable/disable + if (parentItem.isCreateMode() && disableOnCreate) { + setEnabled(false); + } else { + setEnabled(true); + } + + setItemState(); + + AdvancedViewItem item = parentItem.getLineLabelViewItem().getItem( + CommonItemListFactory.ITEM_NET_CONNECT_TYPE); + if (item != null && item instanceof NetConnectTypeViewItem) { + String connectType = ((NetConnectTypeViewItem) item).getValue(); + resetCheckbox(connectType); + } + + // Set checkbox state. + if (modifyEnable) { + modifyCheckBox.setChecked(newValue); + } + setIPInfoEnabled(newValue ? false : true); // Disabled if "checked". + + return false; + } + + @Override + protected void addCheckboxListener() { + modifyCheckBox.addSelectionListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + if (modifyCheckBox.isSelection()) { + newValue = true; + setIPInfoEnabled(false); + + } else { // ON + newValue = false; + setIPInfoEnabled(true); + } + + if (parentItem.isCreateMode()) { + parentItem.getListener().changeConfirmButton(); + } else { + parentItem.getListener().changeConfirmButton( + parentItem.getThis()); + } + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + // TODO Auto-generated method stub + } + }); + } + + public void setIPInfoEnabled(boolean enabled) { + // Enable/Disable ip setting + if (parentItem instanceof NetIPInfoViewItem) { + NetIPInfoViewItem item = (NetIPInfoViewItem)parentItem; + for (ISubViewItem subItem : item.getSubItemList()) { + if (subItem instanceof TextSubViewItem) { + ((TextSubViewItem) subItem).resetText(enabled); + } + } + } + // Enable/Disable DNS setting + AdvancedViewItem dnsItem = parentItem.getLineLabelViewItem().getItem(CommonItemListFactory.ITEM_NET_DNS); + if (dnsItem != null && dnsItem instanceof TextViewItem) { + ((TextViewItem) dnsItem).resetText(enabled); + } + } + +} diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoTextSubViewItem.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoTextSubViewItem.java new file mode 100644 index 0000000..dea4727 --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoTextSubViewItem.java @@ -0,0 +1,215 @@ +/* + * Emulator Manager + * + * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * JiHye Kim + * SeokYeon Hwang + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.ui.detail.item.property; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.widgets.Composite; +import org.tizen.emulator.manager.tool.TapUtil; +import org.tizen.emulator.manager.ui.detail.item.AdvancedViewItem; +import org.tizen.emulator.manager.ui.detail.item.CommonItemListFactory; +import org.tizen.emulator.manager.ui.detail.item.ItemChangeState; +import org.tizen.emulator.manager.ui.detail.item.template.LabelViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.TextSubViewItem; +import org.tizen.emulator.manager.vms.VMPropertyValue; +import org.tizen.emulator.manager.vms.xml.template.Item; + +public class NetIPInfoTextSubViewItem extends TextSubViewItem { + + private boolean alreadyChanged; + + public void setAlreadyChanged(boolean changed) { + alreadyChanged = changed; + } + + public NetIPInfoTextSubViewItem(LabelViewItem parentItem, Composite comp, + Item template) { + super(parentItem, comp, template); + } + + public void setText(String value) { + oldValue = newValue = value; + text.setText(value); + } + + public void resetText(String connectType) { + if (connectType.equals(NetConnectTypeViewItem.VALUE_NAT)) { + resetText(false); + + } else if (connectType.equals(NetConnectTypeViewItem.VALUE_BRIDGE)) { + resetText(true); + } + } + + @Override + public boolean settingModifyItem(VMPropertyValue value) { + + if (parentItem.isCreateMode()) { + oldValue = newValue = ""; + } else { + if (!alreadyChanged) { + oldValue = newValue = value.getAdvancedOptionSubValue( + parentItem.getName(), name); + } + } + text.setText(newValue); + + AdvancedViewItem item = parentItem.getLineLabelViewItem().getItem( + CommonItemListFactory.ITEM_NET_CONNECT_TYPE); + if (item != null && item instanceof NetConnectTypeViewItem) { + String connectType = ((NetConnectTypeViewItem) item).getValue(); + resetText(connectType); + } + + // If use DHCP, set disabled. + if (parentItem instanceof NetIPInfoViewItem) { + NetIPInfoViewItem netItem = (NetIPInfoViewItem)parentItem; + if (netItem.useDHCP()) { + setEnabled(false); + } + } + + return false; + } + + @Override + protected void addListener() { + + text.addFocusListener(new FocusListener() { + + @Override + public void focusGained(FocusEvent arg0) { + if (name.equals(NetIPInfoViewItem.ITEM_IP)) { + String tapName = null; + AdvancedViewItem item = parentItem.getLineLabelViewItem() + .getItem(CommonItemListFactory.ITEM_NET_TAP_DEVICE); + if (item instanceof NetTapDeviceViewItem) + tapName = ((NetTapDeviceViewItem) item).getValue(); + + // Set subnet, gateway if empty. + if (tapName != null || tapName.isEmpty()) { + item = parentItem.getLineLabelViewItem().getItem( + CommonItemListFactory.ITEM_NET_IP_INFO); + if (item instanceof NetIPInfoViewItem) { + NetIPInfoViewItem netIPItem = (NetIPInfoViewItem) item; + String subnet = TapUtil.getNetmaskFromTap(tapName); + if (subnet == null || subnet.isEmpty()) { + netIPItem.setSubnet(""); + + } else if (netIPItem.getSubnet() == null + || netIPItem.getSubnet().isEmpty()) { + netIPItem.setSubnet(subnet); + } + + String gateway = TapUtil.getGatewayFromTap(tapName); + if (gateway == null || gateway.isEmpty()) { + netIPItem.setGateway(""); + + } else if (netIPItem.getGateway() == null + || netIPItem.getGateway().isEmpty()) { + netIPItem.setGateway(gateway); + } + } + + // Set DNS if empty. + item = parentItem.getLineLabelViewItem().getItem( + CommonItemListFactory.ITEM_NET_DNS); + if (item instanceof NetDnsViewItem) { + NetDnsViewItem netDnsItem = (NetDnsViewItem)item; + String dns = TapUtil.getDnsFromTap(tapName); + if (dns == null || dns.isEmpty()) { + netDnsItem.setDns(""); + + } else if (netDnsItem.getDns() == null || netDnsItem.getDns().isEmpty()) { + netDnsItem.setDns(dns); + } + } + } + } + // TODO Auto-generated method stub + + } + + @Override + public void focusLost(FocusEvent arg0) { + // TODO Auto-generated method stub + + } + }); + + text.addModifyListener(new ModifyListener() { + @Override + public void modifyText(ModifyEvent e) { + newValue = text.getText(); + if (parentItem.isCreateMode()) { + parentItem.getListener().changeConfirmButton(); + } else { + parentItem.getListener().changeConfirmButton( + parentItem.getThis()); + } + } + }); + + text.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent event) { + switch (event.keyCode) { + case SWT.CR: + case SWT.KEYPAD_CR: + // TODO + parentItem.getListener() + .ChangeState(ItemChangeState.CREATE); + break; + case SWT.ESC: + parentItem.getListener() + .ChangeState(ItemChangeState.CANCEL); + break; + default: + // TODO + break; + } + } + }); + } + + public String getValue() { + return newValue; + } + + public boolean checkValue() { + return TapUtil.checkIPString(text.isEnabled(), newValue, title); + } + +} diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoViewItem.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoViewItem.java new file mode 100644 index 0000000..8156195 --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetIPInfoViewItem.java @@ -0,0 +1,199 @@ +/* + * Emulator Manager + * + * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * JiHye Kim + * SeokYeon Hwang + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.ui.detail.item.property; + +import java.util.ArrayList; +import java.util.List; + +import org.tizen.emulator.manager.ui.detail.item.LineLabelViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.CheckSubViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.ISubViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.LabelViewItem; +import org.tizen.emulator.manager.vms.xml.template.Item; + +public class NetIPInfoViewItem extends LabelViewItem { + + public static final String ITEM_IP = "ipAddr"; + public static final String ITEM_SUBNET = "subnet"; + public static final String ITEM_GATEWAY = "gateway"; + public static final String ITEM_USE_DHCP = "useDHCP"; + + public boolean useDHCP() { + boolean useDHCP = false; + for (ISubViewItem subItem : getSubItemList()) { + if (subItem.getName().equals(ITEM_USE_DHCP)) { + useDHCP = ((NetIPInfoDHCPSubViewItem) subItem).getValue(); + } + } + return useDHCP; + } + + public void setIPAddress(String ipAddress) { + for (ISubViewItem subItem : getSubItemList()) { + if (subItem.getName().equals(ITEM_IP)) { + ((NetIPInfoTextSubViewItem) subItem).setText(ipAddress); + } + } + } + + public void setSubnet(String subnet) { + for (ISubViewItem subItem : getSubItemList()) { + if (subItem.getName().equals(ITEM_SUBNET)) { + ((NetIPInfoTextSubViewItem) subItem).setText(subnet); + } + } + } + + public void setGateway(String gateway) { + for (ISubViewItem subItem : getSubItemList()) { + if (subItem.getName().equals(ITEM_GATEWAY)) { + ((NetIPInfoTextSubViewItem) subItem).setText(gateway); + } + } + } + + public String getIpAddress() { + for (ISubViewItem subItem : getSubItemList()) { + if (subItem.getName().equals(ITEM_IP)) { + return ((NetIPInfoTextSubViewItem) subItem).getValue(); + } + } + return null; + } + + public String getSubnet() { + for (ISubViewItem subItem : getSubItemList()) { + if (subItem.getName().equals(ITEM_SUBNET)) { + return ((NetIPInfoTextSubViewItem) subItem).getValue(); + } + } + return null; + } + + public String getGateway() { + for (ISubViewItem subItem : getSubItemList()) { + if (subItem.getName().equals(ITEM_GATEWAY)) { + return ((NetIPInfoTextSubViewItem) subItem).getValue(); + } + } + return null; + } + + public NetIPInfoViewItem(Item template, LineLabelViewItem lineLabelViewItem) { + super(template, lineLabelViewItem); + lineLabelViewItem.addItem(name, this); + } + + public void resetSubItems(String connectType) { + for (ISubViewItem subItem : getSubItemList()) { + if (subItem instanceof NetIPInfoTextSubViewItem) { + ((NetIPInfoTextSubViewItem) subItem).resetText(connectType); + + } else if (subItem instanceof NetIPInfoDHCPSubViewItem) { + ((NetIPInfoDHCPSubViewItem) subItem).resetCheckbox(connectType); + } + } + if (connectType.equals(NetConnectTypeViewItem.VALUE_NAT)) { + // TODO hide + // hideItem(); + } else { + // TODO show + // showItem(); + } + } + + @Override + protected List getSubItemList() { + if (subItemList == null) { + subItemList = new ArrayList(); + + List itemList = template.getItem(); + int i = 0; + for (Item item : itemList) { + String itemName = item.getName(); + if (itemName.equals(ITEM_USE_DHCP)) { + subItemList.add(new NetIPInfoDHCPSubViewItem(this, compList + .get(i++), item, true)); + + } else if (itemName.equals(ITEM_IP)) { + subItemList.add(new NetIPInfoTextSubViewItem(this, compList + .get(i++), item)); + + } else if (itemName.equals(ITEM_SUBNET)) { + subItemList.add(new NetIPInfoTextSubViewItem(this, compList + .get(i++), item)); + + } else if (itemName.equals(ITEM_GATEWAY)) { + subItemList.add(new NetIPInfoTextSubViewItem(this, compList + .get(i++), item)); + } + } + } + return subItemList; + } + + @Override + protected int getSubItemCount(Item template) { + List itemList = template.getItem(); + int count = 0; + for (Item item : itemList) { + String itemName = item.getName(); + if (itemName.equals(ITEM_USE_DHCP)) { + count++; + } else if (itemName.equals(ITEM_IP)) { + count++; + } else if (itemName.equals(ITEM_SUBNET)) { + count++; + } else if (itemName.equals(ITEM_GATEWAY)) { + count++; + } + } + return count; + } + + @Override + public boolean checkValue() { + boolean isValid = true; + + for (ISubViewItem subItem : getSubItemList()) { + if (subItem instanceof NetIPInfoTextSubViewItem) { + isValid = ((NetIPInfoTextSubViewItem) subItem).checkValue(); + if (!isValid) { + break; + } + } + } + + return isValid; + + } + +} diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetMacViewItem.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetMacViewItem.java new file mode 100644 index 0000000..05df721 --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetMacViewItem.java @@ -0,0 +1,135 @@ +/* + * Emulator Manager + * + * Copyright (C) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * JiHye Kim + * SeokYeon Hwang + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.ui.detail.item.property; + +import java.util.Random; +import org.tizen.emulator.manager.ui.detail.item.AdvancedViewItem; +import org.tizen.emulator.manager.ui.detail.item.CommonItemListFactory; +import org.tizen.emulator.manager.ui.detail.item.LineLabelViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.TextViewItem; +import org.tizen.emulator.manager.vms.EmulatorVMList; +import org.tizen.emulator.manager.vms.VMProperty; +import org.tizen.emulator.manager.vms.VMPropertyValue; +import org.tizen.emulator.manager.vms.xml.template.Item; + +public class NetMacViewItem extends TextViewItem { + + public NetMacViewItem(Item template, LineLabelViewItem lineLabelViewItem) { + super(template, lineLabelViewItem); + lineLabelViewItem.addItem(name, this); + } + + public void resetText(String connectType) { + + if (connectType.equals(NetConnectTypeViewItem.VALUE_NAT)) { + oldValue = newValue; + newValue = ""; + + } else if (connectType.equals(NetConnectTypeViewItem.VALUE_BRIDGE)) { + if (isCreateMode()) { + oldValue = newValue = getMacAddress(); + + } else { + if (oldValue.isEmpty()) { + oldValue = newValue = getMacAddress(); + + } else { + newValue = oldValue; + } + } + } + text.setText(newValue); + } + + @Override + public boolean settingModifyItem(VMPropertyValue value) { + + text.setEnabled(false); + if (isCreateMode()) { + oldValue = newValue = ""; + } else { + oldValue = newValue = value.getAdvancedOptionValue(name); + } + text.setText(newValue); + + AdvancedViewItem item = lineLabelViewItem + .getItem(CommonItemListFactory.ITEM_NET_CONNECT_TYPE); + if (item != null && item instanceof NetConnectTypeViewItem) { + String connectType = ((NetConnectTypeViewItem) item).getValue(); + resetText(connectType); + } + + return false; + } + + // Return random mac address. (Not duplicated with another VM) + public static String getMacAddress() { + + String newMacAddr; + boolean isDuplicated = false; + + do { + newMacAddr = genetateRandomMacAddr(); + for (VMProperty prop : (VMProperty[]) EmulatorVMList.getInstance() + .getProperties()) { + if (newMacAddr.equals(prop.getPropertyValue() + .getAdvancedOptionValue(CommonItemListFactory.ITEM_NET_MAC))) { + isDuplicated = true; + break; + } + } + } while (isDuplicated); + + return newMacAddr; + } + + private static String genetateRandomMacAddr() { + String[] MAC_CHAR = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", + "A", "B", "C", "D", "E", "F" }; + Random rd = new Random(); + rd.nextInt(15); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 12; i++) { + sb.append(MAC_CHAR[rd.nextInt(15)]); + if (i == 1) { // First 2 byte must be EVEN. + if ((Integer.parseInt(sb.toString(), 16) % 2) == 1) { + sb.delete(0, sb.length()); + i = -1; + continue; + } + } + if (i > 0 && i < 11 && (i % 2 == 1)) { + sb.append(":"); + } + } + return sb.toString(); + } +} diff --git a/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetProxyViewItem.java b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetProxyViewItem.java new file mode 100644 index 0000000..22416e7 --- /dev/null +++ b/common-project/src/org/tizen/emulator/manager/ui/detail/item/property/NetProxyViewItem.java @@ -0,0 +1,390 @@ +/* + * Emulator Manager + * + * Copyright (C) 2014 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Minkee Lee + * SeokYeon Hwang + * Sangho Park + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +package org.tizen.emulator.manager.ui.detail.item.property; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.layout.FormAttachment; +import org.eclipse.swt.layout.FormData; +import org.eclipse.swt.layout.FormLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.tizen.emulator.manager.resources.FontResources; +import org.tizen.emulator.manager.resources.ImageResources; +import org.tizen.emulator.manager.resources.PatchImageResources; +import org.tizen.emulator.manager.ui.MainDialog; +import org.tizen.emulator.manager.ui.detail.item.CommonItemListFactory; +import org.tizen.emulator.manager.ui.detail.item.LineLabelViewItem; +import org.tizen.emulator.manager.ui.detail.item.template.ComboViewItem; +import org.tizen.emulator.manager.ui.widgets.ImageCombo; +import org.tizen.emulator.manager.ui.widgets.WSTATE; +import org.tizen.emulator.manager.vms.VMPropertyValue; +import org.tizen.emulator.manager.vms.helper.HelperClass; +import org.tizen.emulator.manager.vms.xml.template.Item; +import org.tizen.emulator.manager.vms.xml.template.Option; +import org.tizen.emulator.manager.ui.dialog.ProxyDialog; + +public class NetProxyViewItem extends ComboViewItem { + + private String httpProxy = ""; + private String httpsProxy = ""; + private String ftpProxy = ""; + private String socksProxy = ""; + + public final String ITEM_PROXY = CommonItemListFactory.ITEM_NET_PROXY; + public static final String ITEM_PROXY_MODE = "proxyMode"; + public static final String ITEM_HTTP_PROXY = "httpProxy"; + public static final String ITEM_HTTPS_PROXY = "httpsProxy"; + public static final String ITEM_FTP_PROXY = "ftpProxy"; + public static final String ITEM_SOCKS_PROXY = "socksProxy"; + public static final String MODE_NONE = "none"; + public static final String MODE_AUTO = "auto"; + public static final String MODE_MANUAL = "manual"; + + private Map optionMap; + + Button button; + protected static int BUTTON_WIDTH = 49; + protected static int BUTTON_HEIGHT = 21; + protected static int COMBOBOX_WIDTH_MODIFY = 123; + + public String getProxyMode() { + return newValue; + } + + public void setProxyMode(String proxyMode) { + oldValue = newValue = proxyMode; + } + + public String getHttpProxy() { + return httpProxy; + } + + public void setHttpProxy(String httpProxy) { + this.httpProxy = httpProxy; + } + + public String getHttpsProxy() { + return httpsProxy; + } + + public void setHttpsProxy(String httpsProxy) { + this.httpsProxy = httpsProxy; + } + + public String getFtpProxy() { + return ftpProxy; + } + + public void setFtpProxy(String ftpProxy) { + this.ftpProxy = ftpProxy; + } + + public String getSocksProxy() { + return socksProxy; + } + + public void setSocksProxy(String socksProxy) { + this.socksProxy = socksProxy; + } + + public NetProxyViewItem(Item template, LineLabelViewItem lineLabelViewItem) { + super(template, lineLabelViewItem); + + } + + @Override + protected void parseOption(List