Index: trunk/desktop/ICSSerialPort.rbbas
===================================================================
--- trunk/desktop/ICSSerialPort.rbbas	(revision 25)
+++ trunk/desktop/ICSSerialPort.rbbas	(revision 26)
@@ -455,7 +455,234 @@
 	#tag EndMethod
 
+	#tag Method, Flags = &h0
+		Sub exportProfile(theName as string)
+		  dim xml as XmlDocument
+		  dim root as XmlNode
+		  dim rootchild as XmlNode
+		  dim rs as RecordSet
+		  dim i as integer
+		  dim dlg as SaveAsDialog
+		  dim f as FolderItem
+		  
+		  dlg = New SaveAsDialog
+		  dlg.Title = "Export your profile"
+		  #if TargetLinux
+		    dlg.InitialDirectory = SpecialFolder.Home
+		  #else
+		    dlg.InitialDirectory = SpecialFolder.Documents
+		  #endif
+		  
+		  dlg.SuggestedFileName = "ics_"+theName+".xml"
+		  f = dlg.ShowModal()
+		  
+		  if f <> Nil then
+		    //Nothing to see here, move along
+		  else
+		    MsgBox "You must choose a file"
+		    Return
+		  end if
+		  
+		  //Find the record
+		  rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'")
+		  
+		  if rs <> Nil then
+		    //We have a record, so do something
+		    
+		    //Create the root element of the XML file
+		    xml = New XmlDocument
+		    root = xml.AppendChild(xml.CreateElement("icsprofile"))
+		    
+		    //Itterate all available fields and write them to the xml document
+		    for i = 1 to rs.FieldCount
+		      if rs.IdxField(i).Name <> "" and rs.IdxField(i).StringValue<> "" then
+		        
+		        rootchild = root.AppendChild(xml.CreateElement(rs.IdxField(i).Name))
+		        rootchild.AppendChild(xml.CreateTextNode(rs.IdxField(i).StringValue))
+		      end if
+		    next
+		    
+		    //Write the document to a file
+		    xml.SaveXml(f)
+		  else
+		    MsgBox "No profile to export"
+		    Return
+		  end if
+		  
+		End Sub
+	#tag EndMethod
+
+	#tag Method, Flags = &h0
+		Function importProfile() As Boolean
+		  dim success as boolean
+		  dim dlg as OpenDialog
+		  dim f as FolderItem
+		  dim xdoc as XmlDocument
+		  dim root as XmlNode
+		  dim i as integer
+		  dim count as Integer
+		  dim item as string
+		  dim rec as DatabaseRecord
+		  dim theName as string
+		  
+		  rec = New DatabaseRecord
+		  success = False
+		  
+		  
+		  //Get the user to select a file
+		  dlg = New OpenDialog
+		  dlg.Title = "Select a profile to import"
+		  #if TargetLinux
+		    dlg.InitialDirectory = SpecialFolder.Home
+		  #else
+		    dlg.InitialDirectory = SpecialFolder.Documents
+		  #endif
+		  f = dlg.ShowModal()
+		  
+		  //If we have a valid file we need to read it as an xml file to process it
+		  //Items are individually specified to prevent the program from trying to insert columns that do not exist
+		  //All column names should match the database table
+		  
+		  if f <> nil then
+		    
+		    //Read the XML file
+		    xdoc = New XmlDocument(f)
+		    
+		    count = xdoc.DocumentElement.ChildCount
+		    
+		    for i = 0 to count - 1
+		      root = xdoc.DocumentElement.Child(i)
+		      
+		      item = root.FirstChild.Value
+		      
+		      select case root.Name
+		        
+		      case "name"
+		        theName = item
+		      case "cartype"
+		        carType = item
+		      case "byte01"
+		        byte01 = chrb(val(item))
+		      case "byte02"
+		        byte02 = chrb(val(item))
+		      case "byte03"
+		        byte03 = chrb(val(item))
+		      case "byte04"
+		        byte04 = chrb(val(item))
+		      case "byte05"
+		        byte05 = chrb(val(item))
+		      case "byte06"
+		        byte06 = chrb(val(item))
+		      case "byte07"
+		        byte07 = chrb(val(item))
+		      case "byte08"
+		        byte08 = chrb(val(item))
+		      case "byte09"
+		        byte09 = chrb(val(item))
+		      case "byte10"
+		        byte10 = chrb(val(item))
+		      case "byte11"
+		        byte11 = chrb(val(item))
+		      case "byte12"
+		        byte12 = chrb(val(item))
+		      case "byte13"
+		        byte13 = chrb(val(item))
+		      case "byte14"
+		        byte14 = chrb(val(item))
+		      case "byte15"
+		        byte15 = chrb(val(item))
+		      case "byte16"
+		        byte16 = chrb(val(item))
+		      case "byte17"
+		        byte17 = chrb(val(item))
+		      case "byte18"
+		        byte18 = chrb(val(item))
+		        
+		      end select
+		      
+		    next
+		    
+		  else
+		    //File not select we can stop
+		    success = False
+		    return success
+		  end if
+		  
+		  //Now check to make sure their is not an existing profile with this name
+		  dim rs as RecordSet
+		  
+		  rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name= '"+theName+"'")
+		  
+		  if rs <> Nil then
+		    
+		    if rs.RecordCount > 0 then
+		      //we have a duplicate record
+		      //we need to prompt the user about what to do
+		      dim d as New MessageDialog
+		      dim b as MessageDialogButton
+		      d.Icon = MessageDialog.GraphicCaution
+		      d.ActionButton.Caption = "Yes"
+		      d.CancelButton.Visible = True
+		      d.CancelButton.Caption = "No"
+		      d.Message = "A profile exists with this name, do you want to overwrite the existing profile?"
+		      b = d.ShowModal
+		      
+		      //Now determine what the user chose
+		      Select Case b
+		      case d.ActionButton
+		        //The user wants to overwrite the record so we can just save what we have
+		        saveProfile(theName)
+		        success = True
+		        //Values changed because of the overwrite
+		        valuesChanged()
+		      case d.CancelButton
+		        //The user chose not to overwrite the record, we need to come up with a new name
+		        //We will increment the number until we find a name not used or we hit 32 just in case an infinite loop would occur
+		        dim tempString as string
+		        dim tempName as string
+		        i = 0
+		        while rs.RecordCount > 0
+		          i = i + 1
+		          tempString = str(i)
+		          tempName = theName + tempString
+		          //Check to make sure we are not in an infinite looooooooop
+		          //If we are something went wrong
+		          if i > 31 then
+		            exit While
+		          end if
+		          rs = fsicsdb.SQLSelect("SELECT * FROM carprofiles WHERE name = '"+tempName+"'")
+		          
+		        wend
+		        //OK we have a unique name at this point
+		        theName = theName + tempString
+		        saveProfile(theName)
+		        success = True
+		        //new profile was imported
+		        profileImported(theName)
+		      end select
+		      
+		    else
+		      //No duplicate exists so we can just insert the record
+		      saveProfile(theName)
+		      success = True
+		      //new profile was imported
+		      profileImported(theName)
+		    end if
+		    //Close our record
+		    rs.Close
+		    
+		  end if
+		  
+		  Return success
+		  
+		End Function
+	#tag EndMethod
+
 
 	#tag Hook, Flags = &h0
 		Event valuesChanged()
+	#tag EndHook
+
+	#tag Hook, Flags = &h0
+		Event profileImported(profileName as string)
 	#tag EndHook
 
