Skip to content

Commit

Permalink
unix and windows finished
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoMeil committed Mar 5, 2018
1 parent a5e05f2 commit 57e5454
Show file tree
Hide file tree
Showing 29 changed files with 2,280 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
build/
build/
node_modules/
4 changes: 2 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"targets": [
{
"target_name": "posix_print",
"sources": ["./lib/methods.cpp", "./lib/API.cpp"],
"target_name": "unix_printer",
"sources": ["./lib/unix/methods.cpp", "./lib/unix/API.cpp"],
'cflags': ['-fexceptions'],
'cflags_cc': ['-fexceptions'],
"conditions": [
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if(process.platform == 'win32')
module.exports = new (require('./src/windows_printer'))();
else
module.exports = new (require('./src/unix_printer'))();
11 changes: 11 additions & 0 deletions install/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const win = require('./install_win.js');
const unix = require('./install_unix.js');

var platform = process.platform;

if(platform == 'win32'){
win();
}
else{
unix();
}
28 changes: 28 additions & 0 deletions install/install_unix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { spawnSync } = require('child_process');

module.exports = function(){
var cmd,
output,
platform = process.platform;

if(platform == 'darwin')//MacOS
cmd = spawnSync('pkgutil', ['--pkgs'])
else
cmd = spawnSync('dpkg', ['-l']);

if(cmd.stderr.toString('utf-8')){
console.log("Error: ");
console.log(cmd.stderr.toString('utf-8'));
process.exit(1);
}

output = cmd.stdout.toString('utf-8');

if(output.includes('libcups2-dev')){
console.log('libcups2-dev found!');
}
else{
console.error('libcups2-dev is required but was not found');
process.exit(1);
}
}
16 changes: 16 additions & 0 deletions install/install_win.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { spawnSync } = require('child_process');

module.exports = function(){

var cmd, output;
var electron = process.argv.includes('--electron');

cmd = spawnSync(`npm list ${electron ? 'electron-' : ''}edge`);

output = cmd.stdout.toString('utf-8');

if(output.includes('empty')){
console.error(`${electron ? 'electron-' : ''}edge is required but not found`);
process.exit(1);
}
}
9 changes: 6 additions & 3 deletions lib/API.cpp → lib/unix/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace API{

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

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

Expand All @@ -66,7 +66,7 @@ namespace API{
CUPSOptions->Set(UTF8_STRING(options[i].name), UTF8_STRING(options[i].value));
}

result->Set(UTF8_STRING("infos"), infos);
// result->Set(UTF8_STRING("infos"), infos);
result->Set(UTF8_STRING("jobs"), jobs);
result->Set(UTF8_STRING("CUPSOptions"), CUPSOptions);

Expand All @@ -86,7 +86,7 @@ namespace API{
if(dest == NULL)
THROW_EXCEPTION("Printer not found");

const char* filename = cupsGetPPD(*printer);
const char* filename = cupsGetPPD(dest->name);
ppd_file_t* ppd = ppdOpenFile(filename);

ppdMarkDefaults(ppd);
Expand Down Expand Up @@ -146,6 +146,9 @@ namespace API{
string file(*(String::Utf8Value(args[1]->ToString())));
string options(*(String::Utf8Value(args[2]->ToString())));

cups_dest_t* dest = getPrinter(printer.c_str());
printer = string(dest->name);

string cmd = "lp -d " + printer + " " + file + " " + options;

string result = exec(cmd.c_str());
Expand Down
File renamed without changes.
27 changes: 22 additions & 5 deletions lib/methods.cpp → lib/unix/methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ namespace methods{
{IPP_JOB_STOPPED, "stopped"}
};

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

cups_dest_t* result;

if(printer != NULL && strlen(printer) > 0)
result = cupsGetDest(printer, NULL, num_dests, dests);

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);
else
result = cupsGetDest(cupsGetDefault(), NULL, num_dests, dests);
}
else
result = cupsGetDest(cupsGetDefault(), NULL, num_dests, dests);
result = cupsGetDest(cupsGetDefault(), NULL, num_dests, dests);

return result;
}
Expand Down Expand Up @@ -79,4 +85,15 @@ namespace methods{

return result;
}

string strtolower(string data){
transform(data.begin(), data.end(), data.begin(), ::tolower);
return data;
}

const char* strtolower(char* str){
string data(str);
transform(data.begin(), data.end(), data.begin(), ::tolower);
return data.c_str();
}
}
18 changes: 17 additions & 1 deletion lib/methods.hpp → lib/unix/methods.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <cups/cups.h>
#include <cups/ppd.h>
#include <cups/ipp.h>

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -36,7 +38,7 @@ namespace methods{
* Return printer infos. If passed name is invalid, this method will return default printer infos
* @param printer printer name
*/
cups_dest_t* getPrinter(char*);
cups_dest_t* getPrinter(const char*);

/**
* Return a pointer to the list of all printer_job.
Expand All @@ -51,5 +53,19 @@ namespace methods{
* @return output string
*/
string exec(const char*);

/**
* Transform a string to lowercase
* @param data string to transform
* @return string lowercased
*/
string strtolower(string);

/**
* Transform a char* to lowercase
* @param data char* to transform
* @return char* string lowercased
*/
const char* strtolower(char*);
}

Binary file added lib/windows/PdfiumViewer.dll
Binary file not shown.
Loading

0 comments on commit 57e5454

Please sign in to comment.