C++ Samples
Demonstrates how to create a C++ application using the VideoXpert SDK
CppSamples::Exports::DownloadExport Class Reference

This plugin sample downloads an export from the current system and saves as a file. More...

#include <DownloadExport.h>

Public Member Functions

 DownloadExport (const std::string description)
 
 ~DownloadExport ()
 
CppSamples::Common::PluginRun (CppSamples::Common::DataModel *dataModel) override
 Downloads an export from the current system and saves as a file. More...
 
- Public Member Functions inherited from CppSamples::Common::Plugin
 Plugin (const std::string description)
 
virtual ~Plugin ()
 
std::string GetDescription () const
 Gets the description of this plugin. More...
 
PluginGetParent () const
 Gets the reference to the parent of this plugin. More...
 
void SetParent (Plugin *parent)
 Sets the reference to the parent of this plugin. More...
 

Public Attributes

std::string username
 The user name used to log in to the VideoXpert system. More...
 
std::string password
 The password used to log in to the VideoXpert system. More...
 

Protected Member Functions

bool Download (VxSdk::IVxSystem *vxSystem) const
 Downloads an export from the current system and saves as a file. More...
 

Static Protected Member Functions

static void DisplayExportDetailsOnScreen (VxSdk::VxCollection< VxSdk::IVxExport ** > exportCollection)
 Prints the given collection of exports to the screen. More...
 
static std::string GetExportFormatInString (VxSdk::VxExportFormat::Value expFormat)
 Convert the value of export format to string. More...
 
static VxSdk::VxCollection< VxSdk::IVxExport ** > GetExports (VxSdk::IVxSystem *vxSystem)
 Get a collection of exports from the given VideoExpert system. More...
 
static std::string GetExportStatusInString (VxSdk::VxExportStatus::Value expStatus, bool shortStr)
 Returns the status of the export in string More...
 
static int ProgressFunc (void *ptr, double dltotal, double dlnow)
 Callback method to get the download progress More...
 
static void ShowExportDetails (VxSdk::IVxExport *vxExport)
 Print the details of given export. More...
 
static size_t WriteData (void *ptr, size_t size, size_t nmemb, FILE *stream)
 Callback method for writing export data to file More...
 

Detailed Description

This plugin sample downloads an export from the current system and saves as a file.

Definition at line 12 of file DownloadExport.h.

Inherits CppSamples::Common::Plugin.

Constructor & Destructor Documentation

CppSamples::Exports::DownloadExport::DownloadExport ( const std::string  description)
inline

Definition at line 14 of file DownloadExport.h.

14 : CppSamples::Common::Plugin(description) { }
Plugin is the abstract class which can be a menu item and/or a sample. Every sample and menu item mus...
Definition: Plugin.h:33
CppSamples::Exports::DownloadExport::~DownloadExport ( )
inline

Definition at line 15 of file DownloadExport.h.

15 { }

Member Function Documentation

void CppSamples::Exports::DownloadExport::DisplayExportDetailsOnScreen ( VxSdk::VxCollection< VxSdk::IVxExport ** >  exportCollection)
staticprotected

Prints the given collection of exports to the screen.

Parameters
exportCollectionCollection of exports.

Definition at line 34 of file DownloadExport.cpp.

34  {
35  cout << exportCollection.collectionSize << " exports found.";
36  if (exportCollection.collectionSize == 0)
37  return;
38 
39  // Header Line
40  cout << "\n-----------------------------------------------------------------";
41  for (int i = 0; i < exportCollection.collectionSize; i++) {
42  IVxExport* vxExport = exportCollection.collection[i];
43 
44  // Status to string value
45  VxExportStatus::Value expStatus = vxExport->status;
46  string expStatusInString = "U";
47  switch (expStatus) {
48  case VxExportStatus::Value::kExporting:
49  expStatusInString = "E";
50  break;
51  case VxExportStatus::Value::kSuccessful:
52  expStatusInString = "S";
53  break;
54  case VxExportStatus::Value::kFailed:
55  expStatusInString = "F";
56  break;
57  case VxExportStatus::Value::kPending:
58  expStatusInString = "P";
59  break;
60  case VxExportStatus::Value::kUnknown:
61  default:
62  break;
63  }
64 
65  // Print details of single export
66  cout << "\n\t" << (i + 1) << "\t" << vxExport->name << "\t" << expStatusInString;
67  }
68 
69  // Footer
70  cout << "\n-----------------------------------------------------------------";
71  cout << "\n Status: E-Exporting S-Successful F-Failed P-Pending U-Unknown\n";
72 }
VxExportStatus::Value status
bool CppSamples::Exports::DownloadExport::Download ( VxSdk::IVxSystem vxSystem) const
protected

Downloads an export from the current system and saves as a file.

Parameters
vxSystemPointer to the VideoExpert system.
Returns
True if downloaded successfully; False otherwise.

Definition at line 75 of file DownloadExport.cpp.

75  {
76  // Get a collection of exports from the system
77  VxCollection<IVxExport**> exports = GetExports(vxSystem);
78  // Display the details of exports in the collection on screen.
80 
81  if (exports.collectionSize <= 0)
82  return false;
83 
84  // User selects an export
85  cout << "\n" << "Enter index of export to download [1-" << exports.collectionSize << "] : ";
86  int exportIndex = Utility::ReadInt();
87 
88  // Validate user input
89  if (exportIndex < 1 || exportIndex > exports.collectionSize)
90  return false;
91 
92  // Print details of selected export to delete
93  IVxExport* vxExport = exports.collection[exportIndex - 1];
94  ShowExportDetails(vxExport);
95 
96  // File Location
97  cout << "\n" << "Enter path to save export file: ";
98  string pathToSave = Utility::ReadString();
99 
100  // File Name
101  cout << "\n" << "Enter name to save export file: ";
102  string fileToSave = Utility::ReadString();
103 
104  // Set extention based of export format
105  string fullPath = pathToSave + "/" + fileToSave + ".zip";
106 
107  // Make directory if doesn't exists
108 #ifdef VxSdkInLinux
109  mkdir(pathToSave.c_str(),0777);
110 #else
111  _mkdir(pathToSave.c_str());
112 #endif
113 
114  // Initialize CURL
115  int actualFileSize = vxExport->fileSizeKb;
116  CURL *curl = curl_easy_init();
117  FILE *fp;
118 
119  if (curl) {
120  // Open File for Writing
121 #ifdef VxSdkInLinux
122  fp = fopen(fullPath.c_str(), "wb");
123 #else
124  fopen_s(&fp, fullPath.c_str(), "wb");
125 #endif
126  if (fp == nullptr) {
127  char buff[256];
128 #ifdef VxSdkInLinux
129  *buff = *strerror(errno);
130 #else
131  strerror_s(buff, 100, errno);
132 #endif
133  printf("Error opening file for writing: %s\n", buff);
134  return false;
135  }
136 
137  // Create Header structure with user name and password
138  struct curl_slist *headers = nullptr;
139 
140  string userNameHeader = "X-Serenity-User: " + Utility::Encode(username);
141  string passwordHeader = "X-Serenity-Password: " + Utility::Encode(password);
142  headers = curl_slist_append(headers, userNameHeader.c_str());
143  headers = curl_slist_append(headers, passwordHeader.c_str());
144 
145  // Set options to CURL
146  curl_easy_setopt(curl, CURLOPT_URL, vxExport->dataUri);
147  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
148  curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
149  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
150  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
151  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
152  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
153  curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, ProgressFunc);
154  curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &actualFileSize);
155 
156  // Start Downloading
157  cout << "\n" << "Exporting to " << fullPath << "\n\n";
158  curl_easy_perform(curl);
159  // Cleanup the CURL
160  curl_easy_cleanup(curl);
161  fclose(fp);
162  }
163 
164  // Remove the memory allocated to the collection.
165  delete[] exports.collection;
166 
167  return true;
168 }
char dataUri[512]
static int ProgressFunc(void *ptr, double dltotal, double dlnow)
Callback method to get the download progress
std::string password
The password used to log in to the VideoXpert system.
static size_t WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream)
Callback method for writing export data to file
std::string username
The user name used to log in to the VideoXpert system.
static void DisplayExportDetailsOnScreen(VxSdk::VxCollection< VxSdk::IVxExport ** > exportCollection)
Prints the given collection of exports to the screen.
static VxSdk::VxCollection< VxSdk::IVxExport ** > GetExports(VxSdk::IVxSystem *vxSystem)
Get a collection of exports from the given VideoExpert system.
static void ShowExportDetails(VxSdk::IVxExport *vxExport)
Print the details of given export.
string CppSamples::Exports::DownloadExport::GetExportFormatInString ( VxSdk::VxExportFormat::Value  expFormat)
staticprotected

Convert the value of export format to string.

Definition at line 173 of file DownloadExport.cpp.

173  {
174  string expFormatInString = "Unknown";
175  if (expFormat == VxExportFormat::kMkvZip) {
176  expFormatInString = "MkvZip";
177  }
178 
179  return expFormatInString;
180 }
VxCollection< IVxExport ** > CppSamples::Exports::DownloadExport::GetExports ( VxSdk::IVxSystem vxSystem)
staticprotected

Get a collection of exports from the given VideoExpert system.

Parameters
vxSystemPointer to the VideoExpert system.
Returns
A collection of exports.

Definition at line 183 of file DownloadExport.cpp.

183  {
184  cout << "\n\n" << "Fetching exports from system. Please wait...\n";
185  // Read the size of collection from system.
187  VxResult::Value result = vxSystem->GetExports(exports);
188  if (result == VxResult::kInsufficientSize) {
189  // Allocate memory for the requried collection.
190  exports.collection = new IVxExport*[exports.collectionSize];
191  // Read the collection from system.
192  vxSystem->GetExports(exports);
193  }
194 
195  return exports;
196 }
virtual VxResult::Value GetExports(VxCollection< IVxExport ** > &exportCollection) const =0
string CppSamples::Exports::DownloadExport::GetExportStatusInString ( VxSdk::VxExportStatus::Value  expStatus,
bool  shortStr 
)
staticprotected

Returns the status of the export in string

Parameters
expStatusVxExportStatus::Value to be exported.
shortStrSpecifies whether short or long name.
Returns
The status of the export in string

Definition at line 201 of file DownloadExport.cpp.

201  {
202  string expStatusInString = "U";
203  if (expStatus == VxExportStatus::kExporting) {
204  if (shortStr)
205  expStatusInString = "E";
206  else
207  expStatusInString = "Exporting";
208  }
209  else if (expStatus == VxExportStatus::kSuccessful) {
210  if (shortStr)
211  expStatusInString = "S";
212  else
213  expStatusInString = "Successful";
214  }
215  else if (expStatus == VxExportStatus::kFailed) {
216  if (shortStr)
217  expStatusInString = "F";
218  else
219  expStatusInString = "Failed";
220  }
221  else if (expStatus == VxExportStatus::kPending) {
222  if (shortStr)
223  expStatusInString = "P";
224  else
225  expStatusInString = "Pending";
226  }
227 
228  return expStatusInString;
229 }
int CppSamples::Exports::DownloadExport::ProgressFunc ( void *  ptr,
double  dltotal,
double  dlnow 
)
staticprotected

Callback method to get the download progress

Parameters
ptrCustom data assigned to curl.
dltotalTotal bytes to be downloaded (not used).
dlnowNumber of bytes downloaded.

Definition at line 237 of file DownloadExport.cpp.

237  {
238  // Get the value in the given pointer.
239  int actualSize = *static_cast<int*>(ptr);
240  // Downloaded in bytes, so convert to Kb
241  Utility::ShowProgress("Downloading", static_cast<int>(dlnow) / 1000, actualSize, 50);
242  return 0;
243 }
Plugin * CppSamples::Exports::DownloadExport::Run ( CppSamples::Common::DataModel dataModel)
overridevirtual

Downloads an export from the current system and saves as a file.

Parameters
dataModelInstance of data model.

Implements CppSamples::Common::Plugin.

Definition at line 18 of file DownloadExport.cpp.

18  {
19  username = dataModel->username;
20  password = dataModel->password;
21  if (Download(dataModel->VxSystem)) {
22  Utility::ShowProgress("Completed", 100, 100, 50);
23 
24  cout << "\n\n";
25  // Pause for user input before going back to parent menu.
26  Utility::Pause();
27  }
28 
29  // Return reference of parent plugin to move back to parent menu.
30  return GetParent();
31 }
std::string password
The password used to log in to the VideoXpert system.
Definition: Plugin.h:27
VxSdk::IVxSystem * VxSystem
Represents a VideoXpert system and allows the user to manage the system and devices.
Definition: Plugin.h:17
std::string password
The password used to log in to the VideoXpert system.
std::string username
The user name used to log in to the VideoXpert system.
Definition: Plugin.h:22
Plugin * GetParent() const
Gets the reference to the parent of this plugin.
Definition: Plugin.h:46
bool Download(VxSdk::IVxSystem *vxSystem) const
Downloads an export from the current system and saves as a file.
std::string username
The user name used to log in to the VideoXpert system.
void CppSamples::Exports::DownloadExport::ShowExportDetails ( VxSdk::IVxExport vxExport)
staticprotected

Print the details of given export.

Parameters
vxExportInstance of IVxExport.

Definition at line 246 of file DownloadExport.cpp.

246  {
247  cout << "\n" << " Export you selected: " << "\n";
248  cout << "\n" << "-------------------------------------------------------------------";
249  cout << "\n" << " Name: " << vxExport->name;
250  cout << "\n" << " Size (In Kb): " << vxExport->fileSizeKb;
251  cout << "\n" << " Status: " << GetExportStatusInString(vxExport->status, false);
252  cout << "\n" << " Format: " << GetExportFormatInString(vxExport->format) << "\n";
253  cout << "\n" << "-------------------------------------------------------------------";
254 }
VxExportStatus::Value status
static std::string GetExportStatusInString(VxSdk::VxExportStatus::Value expStatus, bool shortStr)
Returns the status of the export in string
static std::string GetExportFormatInString(VxSdk::VxExportFormat::Value expFormat)
Convert the value of export format to string.
VxExportFormat::Value format
size_t CppSamples::Exports::DownloadExport::WriteData ( void *  ptr,
size_t  size,
size_t  nmemb,
FILE *  stream 
)
staticprotected

Callback method for writing export data to file

Parameters
ptrstream data
sizesize of the data
nmembcount of the data
streamfile pointer to which data is being written

Definition at line 263 of file DownloadExport.cpp.

263  {
264  size_t written = fwrite(ptr, size, nmemb, stream);
265  return written;
266 }

Member Data Documentation

std::string CppSamples::Exports::DownloadExport::password

The password used to log in to the VideoXpert system.

Definition at line 31 of file DownloadExport.h.

std::string CppSamples::Exports::DownloadExport::username

The user name used to log in to the VideoXpert system.

Definition at line 26 of file DownloadExport.h.


The documentation for this class was generated from the following files: