1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5cr.define('print_preview', function() {
6  'use strict';
7
8  /** Namespace that contains a method to parse local print destinations. */
9  function LocalDestinationParser() {};
10
11  /**
12   * Parses a local print destination.
13   * @param {!Object} destinationInfo Information describing a local print
14   *     destination.
15   * @return {!print_preview.Destination} Parsed local print destination.
16   */
17  LocalDestinationParser.parse = function(destinationInfo) {
18    var options = {'description': destinationInfo.printerDescription};
19    if (destinationInfo.printerOptions) {
20      // Convert options into cloud print tags format.
21      options.tags = Object.keys(destinationInfo.printerOptions).map(
22          function(key) {return '__cp__' + key + '=' + this[key];},
23          destinationInfo.printerOptions);
24    }
25    return new print_preview.Destination(
26        destinationInfo.deviceName,
27        print_preview.Destination.Type.LOCAL,
28        print_preview.Destination.Origin.LOCAL,
29        destinationInfo.printerName,
30        false /*isRecent*/,
31        print_preview.Destination.ConnectionStatus.ONLINE,
32        options);
33  };
34
35  function PrivetDestinationParser() {}
36
37  /**
38   * Parses a privet destination as one or more local printers.
39   * @param {!Object} destinationInfo Object that describes a privet printer.
40   * @return {!Array.<!print_preview.Destination>} Parsed destination info.
41   */
42  PrivetDestinationParser.parse = function(destinationInfo) {
43    var returnedPrinters = [];
44
45    if (destinationInfo.hasLocalPrinting) {
46       returnedPrinters.push(new print_preview.Destination(
47           destinationInfo.serviceName,
48           print_preview.Destination.Type.LOCAL,
49           print_preview.Destination.Origin.PRIVET,
50           destinationInfo.name,
51           false /*isRecent*/,
52           print_preview.Destination.ConnectionStatus.ONLINE,
53           { cloudID: destinationInfo.cloudID }));
54    }
55
56    if (destinationInfo.isUnregistered) {
57      returnedPrinters.push(new print_preview.Destination(
58          destinationInfo.serviceName,
59          print_preview.Destination.Type.GOOGLE,
60          print_preview.Destination.Origin.PRIVET,
61          destinationInfo.name,
62          false /*isRecent*/,
63          print_preview.Destination.ConnectionStatus.UNREGISTERED));
64    }
65
66    return returnedPrinters;
67  };
68
69  // Export
70  return {
71    LocalDestinationParser: LocalDestinationParser,
72    PrivetDestinationParser: PrivetDestinationParser
73  };
74});
75