DuplicityInfo.vala 4.07 KB
Newer Older
Michael Terry's avatar
Michael Terry committed
1
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
2
/*
3
    This file is part of Déjà Dup.
4
    For copyright information, see AUTHORS.
5

6
    Déjà Dup is free software: you can redistribute it and/or modify
7 8 9 10
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

11
    Déjà Dup is distributed in the hope that it will be useful,
12 13 14 15 16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
17
    along with Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.
18 19 20 21
*/

using GLib;

22 23
namespace DejaDup {

24 25
public class DuplicityInfo : Object
{
26
  public static const int REQUIRED_MAJOR = 0;
27
  public static const int REQUIRED_MINOR = 6;
28
  public static const int REQUIRED_MICRO = 14;
29

30
  public bool reports_encryption {get; private set; default = false;}
31 32 33 34 35 36 37
  
  static DuplicityInfo info = null;
  public static DuplicityInfo get_default() {
    if (info == null)
      info = new DuplicityInfo();
    return info;
  }
38 39 40
  
  // Returns true if everything is OK.  If false, program will close.  A dialog
  // will already have been thrown up.
41
  public bool check_duplicity_version(out string header, out string msg) {
42
    string output;
43 44
    
    try {
45
      Process.spawn_command_line_sync("duplicity --version", out output, null, null);
46 47
    }
    catch (Error e) {
48
      set_missing_duplicity_error(out header, out msg, e.message);
49 50 51
      return false;
    }
    
52
    var tokens = output.split(" ", 2);
53
    if (tokens == null || tokens[0] == null || tokens[1] == null) {
54
      set_missing_duplicity_error(out header, out msg, null);
55 56 57 58 59 60 61
      return false;
    }
    
    // First token is 'duplicity' and is ignorable.  Second looks like '0.5.03'
    version_string = tokens[1].strip();
    var ver_tokens = version_string.split(".");
    if (ver_tokens == null || ver_tokens[0] == null) {
62
      set_missing_duplicity_error(out header, out msg, null);
63 64
      return false;
    }
65
    major = int.parse(ver_tokens[0]);
66 67
    // Don't error out if no minor or micro.  Duplicity might not have them?
    if (ver_tokens[1] != null) {
68
      minor = int.parse(ver_tokens[1]);
69
      if (ver_tokens[2] != null)
70
        micro = int.parse(ver_tokens[2]);
71 72 73 74
    }
    
    var good_enough = meets_requirements();
    if (!good_enough) {
75
      set_bad_version_error(out header, out msg);
76 77
      return false;
    }
78

79 80
    if (meets_version(0, 6, 15))
      reports_encryption = true;
81

82 83 84
    return true;
  }
  
85 86 87 88 89 90 91 92 93 94
  string version_string = null;
  int major = 0;
  int minor = 0;
  int micro = 0;
  
  bool meets_version(int vmaj, int vmin, int vmic) {
    return (major > vmaj) ||
           (major == vmaj && minor > vmin) ||
           (major == vmaj && minor == vmin && micro >= vmic);
  }
95
  
96 97 98 99
  bool equals_version(int vmaj, int vmin, int vmic) {
    return major == vmaj && minor == vmin && micro == vmic;
  }
  
100
  // Doesn't yet handle a blacklist of versions.  We'll cross that bridge when we come to it
101 102
  bool meets_requirements() {
    return meets_version(REQUIRED_MAJOR, REQUIRED_MINOR, REQUIRED_MICRO);
103 104
  }
  
105 106 107
  void set_missing_duplicity_error(out string header, out string msg, string? msg_in) {
    header = _("Could not run duplicity");
    msg = msg_in;
108 109 110
    if (msg != null)
      msg = msg.chomp() + "\n\n";
    else if (version_string == null)
111
        msg = _("Could not understand duplicity version.\n\n");
112
    else
113
        msg = _("Could not understand duplicity version ‘%s’.\n\n").printf(version_string);
114

115
    msg += _("Without duplicity, Déjà Dup Backup Tool cannot function. It will close now.");
116 117
  }
  
118 119
  void set_bad_version_error(out string header, out string msg) {
    header = _("Duplicity’s version is too old");
120
    msg = _("Déjà Dup Backup Tool requires at least version %d.%d.%.2d of duplicity, but only found version %d.%d.%.2d").printf(REQUIRED_MAJOR, REQUIRED_MINOR, REQUIRED_MICRO, major, minor, micro);
121 122 123
  }
}

124 125
} // end namespace