//####
//# Legal Notice :
//#    Copyright, Harman International, 2015
//#    Private, proprietary information, the sole property of Harman.  The
//#    contents, ideas, and concepts expressed herein are not to be disclosed
//#    except within the confines of a confidential relationship and only
//#    then on a need to know basis.
//#
//#    Any entity in possession of this Harman Software shall not, and shall not
//#    permit any other person to, disclose, display, loan, publish, transfer
//#    (whether by sale, assignment, exchange, gift, operation of law or
//#    otherwise), license, sublicense, copy, or otherwise disseminate this
//#    Harman Software.
//#
//#    This Harman Software is owned by Harman and is protected by United States
//#    copyright laws, patent laws, international treaty provisions, and/or
//#    state of Texas trade secret laws.
//#
//#    Portions of this Harman Software may, from time to time, include
//#    pre-release code and such code may not be at the level of performance,
//#    compatibility and functionality of the final code. The pre-release code
//#    may not operate correctly and may be substantially modified prior to
//#    final release or certain features may not be generally released. Harman is
//#    not obligated to make or support any pre-release code. All pre-release
//#    code is provided "as is" with no warranties.
//#
//#    This Harman Software is provided with restricted rights. Use, duplication,
//#    or disclosure by the Government is subject to restrictions as set forth
//#    in subparagraph (1)(ii) of The Rights in Technical Data and Computer
//#    Software clause at DFARS 252.227-7013 or subparagraphs (1) and (2) of
//#    the Commercial Computer Software Restricted Rights at 48 CFR 52.227-19,
//#    as applicable.
//####

PROGRAM_NAME='Listview Example'
 
DEFINE_DEVICE
dvTP = 10001:1:0
 
DEFINE_CONSTANT
// listview button address
INTEGER btnListview = 11
 
DEFINE_VARIABLE
//just a variable to hold our "published URL" value
CHAR publishedURL[DATA_MAX_VALUE_LENGTH]
//just a variable to hold our recordset ID
CHAR recordsetID[DATA_MAX_ID_LENGTH]
 
DEFINE_FUNCTION GenerateDataFeed()
{
    //we can't add fields to a record or a record to
    // a feed if they don't exist so lets define them here
    
    //DATA_FEED is a predefined structure in the Netlinx AXI
    //datafeed is our variable representing that structure
    STACK_VAR DATA_FEED datafeed
    
    //DATA_RECORD is a predefined structure in the Netlinx AXI
    //record is our variable representing that structure
    STACK_VAR DATA_RECORD record
     
// --------------------------------------------------
// CREATE A NEW DATA FEED
// --------------------------------------------------
    
    //set the characteristics of the dataFeed (these are just descriptive strings)
    //name/description/source are defined in the DATA_FEED structure 
    datafeed.name = 'phonelist'
    datafeed.description = 'Some Harman Employees'
    datafeed.source = 'Netlinx PhoneList'
    
    //we've defined all the values for our DATA_FEED
    //now we need to "create" our DATA_FEED
    DATA_CREATE_FEED(datafeed)
   
    // A recordset id is required for adding records to the feed
    recordsetID = 'recordSetPhoneList'
     
// --------------------------------------------------
// DEFINE AND POPULATE THE DATA FIELDS
// This example will have 10 names in a phone list
// --------------------------------------------------
    // Records can have metadata fields and content fields.  In this
    // example we won't use any metadata
    SET_LENGTH_ARRAY(record.metadata, 1)
    // We will have 3 content fields per record: photo, name and phone number
    SET_LENGTH_ARRAY(record.content, 3)

    // Initialize the field attributes that will be the same for every record   
    // the first field in a record will be the image
    // When mapping the data to a listview widget on the panel and when using
    // the data in the custom event, the id field is used.
    record.content[1].id = 'photo'; 
    record.content[1].type = DATA_TYPE_IMAGE;
    record.content[1].format = DATA_FORMAT_URL;
    // The label can be something different from the id but in our case we'll
    // keep them the same
    record.content[1].label = 'photo';
     
    // The second field in a record will be the name
    record.content[2].id = 'name'; 
    record.content[2].type = DATA_TYPE_STRING;
    record.content[2].format = '';
    record.content[2].label = 'name';
    // The third field will be the phone number
    record.content[3].id = 'number'; 
    record.content[3].type = DATA_TYPE_STRING;
    record.content[3].format = DATA_FORMAT_PHONE;
    record.content[3].label = 'number'; 
    // The next step is to put in the actual values for the 3 fields
    record.content[1].value = 'http://10.0.0.0/CONTACT_1.png'
    record.content[2].value = 'CONTACT_1'
    record.content[3].value = '123-456-7890'
    // Add the record to the feed
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
    // The same record can be reused for the rest of the list
    // Just change the relevant values and add the record to the feed
    record.content[1].value = 'http://10.0.0.0/CONTACT_2.png'
    record.content[2].value = 'CONTACT_2'
    record.content[3].value = '123-456-7891'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_3.png'
    record.content[2].value = 'CONTACT_3'
    record.content[3].value = '123-456-7892'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_4.png'
    record.content[2].value = 'CONTACT_4'
    record.content[3].value = '123-456-7893'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_5.png'
    record.content[2].value = 'CONTACT_5'
    record.content[3].value = '123-456-7894'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_6.png'
    record.content[2].value = 'CONTACT_6'
    record.content[3].value = '123-456-7895'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_7.png'
    record.content[2].value = 'CONTACT_7'
    record.content[3].value = '123-456-7896'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_8.png'
    record.content[2].value = 'CONTACT_8'
    record.content[3].value = '123-456-7897'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_9.png'
    record.content[2].value = 'CONTACT_9'
    record.content[3].value = '123-456-7898'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)
     
    record.content[1].value = 'http://10.0.0.0/CONTACT_10.png'
    record.content[2].value = 'CONTACT_10.'
    record.content[3].value = '123-456-7899'
    DATA_ADD_RECORD(datafeed.name, recordsetID, record)

    // The final step is to publish the feed
    publishedURL = DATA_PUBLISH_FEED(datafeed.name)
} 
DEFINE_START
    GenerateDataFeed()
     
DEFINE_EVENT
DATA_EVENT[dvTP]
{
    ONLINE:
    {
    // Set the URL for the data source for the listviewer in the panel
    SEND_COMMAND dvTP,"'^LVD-',ITOA(btnListview),',',publishedURL"
    // Map the fields in the listviewer to the columns
    SEND_COMMAND dvTP,"'^LVM-',ITOA(btnListview),',i1=${photo}|t1=${name}|t2=${number}'"
    // Sort ascending by name
    SEND_COMMAND dvTP,"'^LVS-',ITOA(btnListview),',${name};a'"
    // Command the listview to load the data from the master
    SEND_COMMAND dvTP,"'^LVR-',ITOA(btnListview)"
    }
}
 
// The custom event that is raised whenever a listview item is selected on the panel
CUSTOM_EVENT[dvTP,btnListview,LISTVIEW_ON_ROW_SELECT_EVENT]
{
    SLONG payloadId
    SLONG payloadType
    //just a char array to hold the data we want to use in the custom event.
    CHAR fields[2][16]
    //char variables to hold our data for "name" & "number"
    CHAR name[DATA_MAX_VALUE_LENGTH]
    CHAR number[DATA_MAX_VALUE_LENGTH]
    
    //variable record, of type DATA_RECORD, to hold the record we retrieve from the custom event
    DATA_RECORD record
    // Get the data access ID from the custom event
    // variable is payloadID - custom.value1 is predefined
    payloadId = custom.value1
    // Get the data type from the custom event
    // variable is payloadType - custom.value2 is predefined
    payloadType = custom.value2
     
    if (payloadId > 0 && payloadType == DATA_STRUCTURE_DATARECORD)
    {
    // Specify which fields we want to retrieve from the payload (these are the ids we defined earlier)
    fields[1] = 'name'
    fields[2] = 'number'
    // Retrieve the record and get our requested fields
    if (DATA_GET_EVENT_RECORD(dvTP, payloadId, fields, record) > 0)
    {
        // The record existed and contained our fields 
        // copy the values that we are interested in 
        name = record.content[1].value
        number = record.content[2].value
        // Send the name & number that was retrieved to 
        // the appropriate buttons & show the popup
        SEND_COMMAND dvTP,"'^TXT-50,0,',name"
        SEND_COMMAND dvTP,"'^TXT-51,0,',number"
        SEND_COMMAND dvTP,"'^PPN-Calling'"
    }
    }
}
(***********************************************************)
(*            THE ACTUAL PROGRAM GOES BELOW                *)
(***********************************************************)
DEFINE_PROGRAM
(***********************************************************)
(*                     END OF PROGRAM                      *)
(*        DO NOT PUT ANY CODE BELOW THIS COMMENT           *)
(***********************************************************)