Skip to content

Commit

Permalink
Fixed memort waste in unix
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoMeil committed Mar 21, 2018
1 parent bda7488 commit 861df83
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 106 deletions.
36 changes: 21 additions & 15 deletions lib/unix/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,38 @@ namespace API{
cups_dest_t* dest = getPrinter(*printer);

if(dest == NULL)
THROW_EXCEPTION("Printer not found");
THROW_EXCEPTION("Printer not found or error retrieving printer");

cups_option_t* options = dest->options;
printer_job* printerJob = getJobs(dest);
cups_job_t* printerJobs;
int num_jobs = cupsGetJobs(&printerJobs, dest->name, 0, CUPS_WHICHJOBS_ALL);

Local<Object> result = Object::New(isolate);

// Local<Object> infos = Object::New(isolate);
Local<Object> jobs = Array::New(isolate);
Local<Object> CUPSOptions = Object::New(isolate);

for(int i = 0; i < dest->num_options; i++){
CUPSOptions->Set(UTF8_STRING(options[i].name), UTF8_STRING(options[i].value));
CUPSOptions->Set(UTF8_STRING(dest->options[i].name), UTF8_STRING(dest->options[i].value));
}

for(int i = 0; i < printerJob->num_jobs; i++){
for(int i = 0; i < num_jobs; i++){
Local<Object> job = Object::New(isolate);
job_info* info = printerJob[i].info;

for(int j = 0; j < printerJob->num_info; j++){
job->Set(UTF8_STRING(info[j].key), UTF8_STRING(info[j].value));
}

job->Set(UTF8_STRING("completed_time"), UTF8_STRING(httpGetDateString(printerJobs[i].completed_time)));
job->Set(UTF8_STRING("creation_time"), UTF8_STRING(httpGetDateString(printerJobs[i].creation_time)));
job->Set(UTF8_STRING("format"), UTF8_STRING(printerJobs[i].format));
job->Set(UTF8_STRING("id"), UTF8_STRING(to_string(printerJobs[i].id).c_str()));
job->Set(UTF8_STRING("priority"), UTF8_STRING(to_string(printerJobs[i].priority).c_str()));
job->Set(UTF8_STRING("processing_time"), UTF8_STRING(httpGetDateString(printerJobs[i].processing_time)));
job->Set(UTF8_STRING("size"), UTF8_STRING(to_string(printerJobs[i].size).c_str()));
job->Set(UTF8_STRING("status"), UTF8_STRING((job_statuses.at(printerJobs[i].state)).c_str()));
job->Set(UTF8_STRING("title"), UTF8_STRING(printerJobs[i].title));
job->Set(UTF8_STRING("user"), UTF8_STRING(printerJobs[i].user));

jobs->Set(i, job);
}

for (int i = 0; i < dest->num_options; i++){
CUPSOptions->Set(UTF8_STRING(options[i].name), UTF8_STRING(options[i].value));
}
cupsFreeJobs(num_jobs, printerJobs);
cupsFreeDests(1, dest);

// result->Set(UTF8_STRING("infos"), infos);
result->Set(UTF8_STRING("jobs"), jobs);
Expand Down Expand Up @@ -121,6 +124,9 @@ namespace API{

group++;
}

cupsFreeDests(1, dest);
ppdClose(ppd);

result->Set(UTF8_STRING("options"), resOptions);
result->Set(UTF8_STRING("defaultOptions"), resDefaults);
Expand Down
11 changes: 11 additions & 0 deletions lib/unix/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ using namespace v8;

//Method declarations
namespace API{

unordered_map<ipp_jstate_t, string> job_statuses = {
{IPP_JOB_ABORTED, "aborted"},
{IPP_JOB_CANCELED, "canceled"},
{IPP_JOB_COMPLETED, "completed"},
{IPP_JOB_HELD, "held"},
{IPP_JOB_PENDING, "pending"},
{IPP_JOB_PROCESSING, "processing"},
{IPP_JOB_STOPPED, "stopped"}
};

/**
* List installed printers
* @return array of printers
Expand Down
84 changes: 48 additions & 36 deletions lib/unix/methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,75 @@ using namespace std;

namespace methods{

unordered_map<ipp_jstate_t, string> job_statuses = {
{IPP_JOB_ABORTED, "aborted"},
{IPP_JOB_CANCELED, "canceled"},
{IPP_JOB_COMPLETED, "completed"},
{IPP_JOB_HELD, "held"},
{IPP_JOB_PENDING, "pending"},
{IPP_JOB_PROCESSING, "processing"},
{IPP_JOB_STOPPED, "stopped"}
};

cups_dest_t* getPrinter(const char* printer){
cups_dest_t* dests;
int num_dests = cupsGetDests(&dests);

cups_dest_t* result;
cups_dest_t* temp;

if(printer != NULL){
string printerName = strtolower(string(printer));

if(strlen(printer) > 0 && printerName.compare("null") != 0 && printerName.compare("undefined") != 0)
result = cupsGetDest(printer, NULL, num_dests, dests);
temp = cupsGetDest(printer, NULL, num_dests, dests);
else
result = cupsGetDest(cupsGetDefault(), NULL, num_dests, dests);
temp = cupsGetDest(cupsGetDefault(), NULL, num_dests, dests);
}
else
result = cupsGetDest(cupsGetDefault(), NULL, num_dests, dests);
temp = cupsGetDest(cupsGetDefault(), NULL, num_dests, dests);

cups_dest_t* result = new cups_dest_t;

if(! copyDest(temp, result))
return NULL;

cupsFreeDests(num_dests, dests);
dests = temp = NULL;

return result;
}

printer_job* getJobs(cups_dest_t* dest){
cups_job_t* jobs;
int totalJobs = cupsGetJobs(&jobs, dest->name, 0, CUPS_WHICHJOBS_ACTIVE);
bool copyDest(cups_dest_t* source, cups_dest_t* dest){
if(source->instance != NULL){
dest->instance = strdup(source->instance);

printer_job* resultJob = new printer_job[totalJobs];
if(dest->instance == NULL)
return false;
}

resultJob->num_jobs = totalJobs;
if(source->name != NULL){
dest->name = strdup(source->name);

for(int i = 0; i < totalJobs; i++){

job_info* info = new job_info[10];

info[0] = {"completed_time", httpGetDateString(jobs->completed_time)};
info[1] = {"creation_time", httpGetDateString(jobs->creation_time)};
info[2] = {"format", jobs->format};
info[3] = {"id", to_string(jobs->id).c_str()};
info[4] = {"priority", to_string(jobs->priority).c_str()};
info[5] = {"processing_time", httpGetDateString(jobs->processing_time)};
info[6] = {"size", to_string(jobs->size).c_str()};
info[7] = {"status", (job_statuses.at(jobs->state)).c_str()};
info[8] = {"title", jobs->title};
info[9] = {"user", jobs->user};

resultJob[i].info = info;
if(dest->name == NULL)
return false;
}

dest->is_default = source->is_default;
dest->num_options = source->num_options;

dest->options = new cups_option_t[source->num_options];

if(! copyOptions(source->options, source->num_options, dest->options)){
cerr << "Errore durante la copia delle opzioni\n";
return 1;
}

return resultJob;
return true;
}

bool copyOptions(cups_option_t* source, int num, cups_option_t* dest){

for(int i = 0; i < num; i++){
dest[i].name = strdup(source[i].name);
dest[i].value = strdup(source[i].value);

if(dest[i].name == NULL)
return false;
if(dest[i].value == NULL)
return false;
}

return true;
}

string exec(const char* cmd) {
Expand Down
39 changes: 15 additions & 24 deletions lib/unix/methods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,13 @@
#include <iostream>
#include <string>
#include <unordered_map>
#include <cstring>
#include <stdexcept>
#include <stdio.h>

using namespace std;

namespace methods{
//structures

/**
* Defines a general info for the job.
* I.e. the status of the job will be represented with job_info status = {"status", "waiting"})
*/
typedef struct job_info{
const char* key;
const char* value;
} job_info;

/**
* Collects all job_infos
*/
typedef struct printer_job{
job_info* info;
int num_info = 10;
int num_jobs;
} printer_job;

//methods

/**
Expand All @@ -41,11 +22,21 @@ namespace methods{
cups_dest_t* getPrinter(const char*);

/**
* Return a pointer to the list of all printer_job.
* @param dest printer from which take the jobs
* @return pointer to printer_job list
* Copies a destination from src to dest
* @param src cups_dest_t* from which to copy informations
* @param dest cups_dest_t* in which to copy informations
* @return copy of src
*/
bool copyDest(cups_dest_t*, cups_dest_t*);

/**
* Copies options from src to dest
* @param src cups_option_t* from which to copy informations
* @param num_options number of options to copy starting from the first
* @param dest cups_option_t* in which to copy informations
* @return copy of src
*/
printer_job* getJobs(cups_dest_t*);
bool copyOptions(cups_option_t*, int, cups_option_t*);

/**
* Alias of system() that return output string
Expand Down
57 changes: 36 additions & 21 deletions lib/windows/src/windows_printer/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.ObjectModel;
using System.Drawing.Printing;
using System.Printing;
using System.Web;

using PdfiumViewer;

namespace windows_printer
Expand Down Expand Up @@ -107,7 +109,7 @@ public static PrinterOptions GetOptions(string printerName)
Resolutions = resolutions
};
}
public static bool PrintPDF(String printer, Settings settings, String filename, short copies)
public static bool Print(String printer, Settings settings, String filename, short copies)
{
PrinterSettings printerSettings = new PrinterSettings { PrinterName = printer };

Expand All @@ -117,9 +119,8 @@ public static bool PrintPDF(String printer, Settings settings, String filename,
printerSettings.Copies = copies;
printerSettings.Collate = settings.Collate;

Duplex d;

if (Enum.TryParse<Duplex>(settings.Duplex, out d))
if (Enum.TryParse<Duplex>(settings.Duplex, out Duplex d))
printerSettings.Duplex = d;

if (settings.FromPage > 0)
Expand All @@ -130,13 +131,15 @@ public static bool PrintPDF(String printer, Settings settings, String filename,

PageSettings pageSettings = new PageSettings(printerSettings)
{
Margins = new Margins(0, 0, 0, 0)
Margins = new Margins(0, 0, 0, 0),
Color = settings.Color,
Landscape = settings.Landscape
};

string paperSize = settings.PaperSize;
if (paperSize.Length > 0)
{
if (paperSize.Contains("Custom"))
if (paperSize.Contains("Custom") || paperSize.Contains("custom"))
{
int dot = paperSize.IndexOf('.');
int x = paperSize.IndexOf('x');
Expand All @@ -157,7 +160,32 @@ public static bool PrintPDF(String printer, Settings settings, String filename,

}
}


string mimeType = MimeMapping.GetMimeMapping(filename);

if (mimeType == "application/pdf")
return PrintPDF(filename, printerSettings, pageSettings);

return false;
}
#endregion

#region PRIVATE_METHODS
private static string[] setInfo<T>(ReadOnlyCollection<T> collection)
{
List<string> temp = new List<string>();
foreach (T value in collection)
{
temp.Add(value.ToString());
}

return temp.ToArray();
}
private static bool PrintPDF(string filename, PrinterSettings printerSettings, PageSettings pageSettings)
{
bool landscape = pageSettings.Landscape,
color = pageSettings.Color;

try
{
using (var document = PdfDocument.Load(filename))
Expand All @@ -168,8 +196,8 @@ public static bool PrintPDF(String printer, Settings settings, String filename,
printDocument.DefaultPageSettings = pageSettings;
printDocument.PrintController = new StandardPrintController();
printDocument.QueryPageSettings += delegate (object sender, QueryPageSettingsEventArgs e) {
e.PageSettings.Landscape = settings.Landscape;
e.PageSettings.Color = settings.Color;
e.PageSettings.Landscape = landscape;
e.PageSettings.Color = color;
};
printDocument.Print();
}
Expand All @@ -184,18 +212,5 @@ public static bool PrintPDF(String printer, Settings settings, String filename,
}
}
#endregion

#region PRIVATE_METHODS
private static string[] setInfo<T>(ReadOnlyCollection<T> collection)
{
List<string> temp = new List<string>();
foreach (T value in collection)
{
temp.Add(value.ToString());
}

return temp.ToArray();
}
#endregion
}
}
1 change: 1 addition & 0 deletions lib/windows/src/windows_printer/windows_printer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Printing" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
Binary file modified lib/windows/windows_printer.dll
Binary file not shown.
Loading

0 comments on commit 861df83

Please sign in to comment.